猿问

将Python中的特殊字符替换为“N/A”

我想将仅包含表情符号的所有行更改为df['Comments'][2]N/A。


df['Comments'][:6]

0                                                          nice

1                                                       Insane3

2                                                          😻😻❤️

3                                                @bertelsen1986

4                       20 or 30 mm rise on the Renthal Fatbar?

5                                     Luckily I have one to 🔥💪🏻

以下代码不会返回我期望的输出:


df['Comments'].replace(';', ':', '!', '*', np.NaN)

预期输出:


df['Comments'][:6]

0                                                          nice

1                                                       Insane3

2                                                          nan

3                                                @bertelsen1986

4                       20 or 30 mm rise on the Renthal Fatbar?

5                                     Luckily I have one to 🔥💪🏻


慕的地6264312
浏览 152回答 2
2回答

人到中年有点甜

函数(remove_emoji)尝试安装第一个emoji库 -pip install emojiimport reimport emojidf.Comments.apply(lambda x: x if (re.sub(r'(:[!_\-\w]+:)', '', emoji.demojize(x)) != "") else np.nan)0                         nice1                      Insane32                          NaN3               @bertelsen19864    Luckily I have one to 🔥💪🏻Name: a, dtype: object

手掌心

您可以通过迭代每行中的 unicode 字符来检测仅包含表情符号的行(使用emoji和unicodedata包):df = {}df['Comments'] = ["Test", "Hello 😉", "😉😉😉"]import unicodedataimport numpy as npfrom emoji import UNICODE_EMOJIfor i in range(len(df['Comments'])):    pure_emoji = True    for unicode_char in unicodedata.normalize('NFC', df['Comments'][i]):        if unicode_char not in UNICODE_EMOJI:            pure_emoji = False            break    if pure_emoji:        df['Comments'][i] = np.NaNprint(df['Comments'])
随时随地看视频慕课网APP

相关分类

Python
我要回答