我需要从数据框中删除表情符号

问题描述

我有一个df,其中包含来自csv文件的推文。我正在尝试删除表情符号以及<>符号中的所有内容,但是我不知道如何将其应用于多于一串文本的字符串,以便所有推文行都去除了表情符号。>

例如,我在df中连续有以下文本:
"@EvilsBadGirl @DefectiveTwinKP Don<U+0092>t really have much of that in general right Now so just because I didn<U+0092>t jump around and get all excited over talking about names for your kids doesn<U+0092>t mean I don<U+0092>t care or don<U+0092>t wanna do it Alright? I<U+0092>m just tired and I<U+0092>m not gonna do the whole <U+0093>You didn<U+0092>t say it you-""

我需要删除<U+0092>和所有其他此类的文本。

我在网上看到的所有内容显示了如何针对一小段文字进行操作,但是如何将其应用于完整的dataframe

我尝试了以下方法,但没有成功:

def remove_emoji(string):
    emoji_pattern = re.compile("["
                           u"\U0001F600-\U0001F64F"  # emoticons
                           u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                           u"\U0001F680-\U0001F6FF"  # transport & map symbols
                           u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                           u"\U00002702-\U000027B0"
                           u"\U000024C2-\U0001F251"
                           "]+",flags=re.UNICODE)
    return emoji_pattern.sub(r'',string)

text = df['text']

df.apply(remove_emoji(df['text'])

解决方法

您需要在要转换的特定列上使用“应用”功能。 另外,您不需要每次调用该函数时都编译正则表达式,只需执行一次即可。

emoji_pattern = re.compile("["
                       u"\U0001F600-\U0001F64F"  # emoticons
                       u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                       u"\U0001F680-\U0001F6FF"  # transport & map symbols
                       u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                       u"\U00002702-\U000027B0"
                       u"\U000024C2-\U0001F251"
                       "]+",flags=re.UNICODE)  

def remove_emoji(string):
    return emoji_pattern.sub(r'',string)


df['text'] = df['text'].apply(remove_emoji) # Apply the remove_emoji function to each row in the text column