从一系列文本中提取表情符号 [英] Extract emoji from series of text

查看:0
本文介绍了从一系列文本中提取表情符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

,而不是迭代整个数据集。您可以使用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 ❤️   ❤

这篇关于从一系列文本中提取表情符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆