Python pandas: 从 DataFrame 中删除表情符号
Python pandas: Remove emojis from DataFrame
我有一个包含许多不同表情符号的数据框,我想删除它们。我查看了类似问题的答案,但它们对我不起作用。
index| messages
----------------
1 |Hello!
2 |Good Morning
3 |How are you ?
4 | Good
5 | Ländern
现在我想从 DataFrame 中删除所有这些表情符号,所以它看起来像这样
index| messages
----------------
1 |Hello!
2 |Good Morning
3 |How are you ?
4 | Good
5 |Ländern
我尝试了这里的解决方案,但不幸的是它也删除了所有非英语字母,如“ä”
我认为以下是对您问题的回答。我添加了一些其他字符以进行验证。
import pandas as pd
df = pd.DataFrame({'messages':['Hello! ', 'Good-Morning ', 'How are you ?', ' Goodé ', 'Ländern' ]})
df['messages'].astype(str).apply(lambda x: x.encode('latin-1', 'ignore').decode('latin-1'))
此解决方案将保留所有 ASCII 和 latin-1 字符,即 this list 中 U+0000 和 U+00FF 之间的字符。对于扩展拉丁语加希腊语,请使用 < 1024
:
df = pd.DataFrame({'messages': ['Länder ❤️', 'Hello! ']})
filter_char = lambda c: ord(c) < 256
df['messages'] = df['messages'].apply(lambda s: ''.join(filter(filter_char, s)))
结果:
messages
0 Länder
1 Hello!
请注意,这不适用于日文文本。另一个问题是,心形“emoji”其实是一个Dingbat so I can't simply filter for the Basic Multilingual Plane的Unicode,哦。
我有一个包含许多不同表情符号的数据框,我想删除它们。我查看了类似问题的答案,但它们对我不起作用。
index| messages
----------------
1 |Hello!
2 |Good Morning
3 |How are you ?
4 | Good
5 | Ländern
现在我想从 DataFrame 中删除所有这些表情符号,所以它看起来像这样
index| messages
----------------
1 |Hello!
2 |Good Morning
3 |How are you ?
4 | Good
5 |Ländern
我尝试了这里的解决方案,但不幸的是它也删除了所有非英语字母,如“ä”
我认为以下是对您问题的回答。我添加了一些其他字符以进行验证。
import pandas as pd
df = pd.DataFrame({'messages':['Hello! ', 'Good-Morning ', 'How are you ?', ' Goodé ', 'Ländern' ]})
df['messages'].astype(str).apply(lambda x: x.encode('latin-1', 'ignore').decode('latin-1'))
此解决方案将保留所有 ASCII 和 latin-1 字符,即 this list 中 U+0000 和 U+00FF 之间的字符。对于扩展拉丁语加希腊语,请使用 < 1024
:
df = pd.DataFrame({'messages': ['Länder ❤️', 'Hello! ']})
filter_char = lambda c: ord(c) < 256
df['messages'] = df['messages'].apply(lambda s: ''.join(filter(filter_char, s)))
结果:
messages
0 Länder
1 Hello!
请注意,这不适用于日文文本。另一个问题是,心形“emoji”其实是一个Dingbat so I can't simply filter for the Basic Multilingual Plane的Unicode,哦。