问题描述
我在从一系列表情中提取表情符号时遇到了问题。 使用的代码:
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)
--> 'U0001f929U0001f929'
预期结果:
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 ❤️ ❤