从一系列文本中提取表情符号

Extract emoji from series of text

我在从系列中提取表情符号时遇到问题。 使用的代码:

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 ❤️                                    | ❤️ 
... ... ...

注意: 我在查看这些链接后才问这个问题:

而不是遍历整个数据集。您可以使用 applylambda.

应用函数
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 ❤️   ❤