如何解决从一系列文字中提取表情符号
我在从系列中提取表情符号时遇到问题。 使用的代码:
import emoji
def extract_emojis(text):
return ''.join(c for c in text if c in emoji.UNICODE_EMOJI)
for text in df['comments']:
df['emoji']=extract_emojis(text)
输出:
comments | emoji
0 Its very beautiful
1 Your new bike,@keir ...?
2 @philip ??
3 Any news on the Canadian expansion mentioned i...
4 Rocky Mountain ❤️
... ... ...
仅检查文本上的功能:
text = '@philip ??'
extract_emojis(text)
--> '\U0001f929\U0001f929'
预期结果:
comments | emoji
0 Its very beautiful |
1 Your new bike,@keir ...? |
2 @philip ?? | ??
3 Any news on the Canadian expansion mentioned i... |
4 Rocky Mountain ❤️ | ❤️
... ... ...
注意:
在查看以下链接后,我才问了这个问题:
Python unicode character conversion for Emoji
How to extract all the emojis from text?
解决方法
而不是遍历整个数据集。您可以使用apply
或lambda
应用该功能。
import pandas as pd
import emoji
df = pd.DataFrame([['@philip ?? '],['Rocky Mountain ❤️']],columns = ['comments'])
使用Lambda:
df['emojis'] = df['comments'].apply(lambda row: ''.join(c for c in row if c in emoji.UNICODE_EMOJI))
df
使用“应用”
def extract_emojis(text):
return ''.join(c for c in text if c in emoji.UNICODE_EMOJI)
df['emoji_apply'] = df['comments'].apply(extract_emojis)
df
输出:
comments emojis
@philip ?? ??
Rocky Mountain ❤️ ❤
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。