首页 > 解决方案 > 从 Pandas Dataframe 中删除 puncts

问题描述

我想使用推文从数据框中删除所有标点符号和拉丁字符以进行情感分析。代码如下。我想从列中删除标点符号,但代码删除了文本,只剩下标点符号!!有什么建议么?

remove_puncts = lambda x: re.sub("[^A-Za-z0-9\s]+", "", str(x))

df['new'] = df.Tweet.apply(remove_puncts)

标签: pythonpandassentiment-analysispunctuation

解决方案


尝试使用pandas.Series.str.replace

df['Tweet'].str.replace(r'[^0-9a-zA-Z\s]+', '', regex=True)

示例输入:

df = pd.DataFrame({'Tweet': ['abc, def; (hij)!?', '[w] x/y: z']})
df
               Tweet
0  abc, def; (hij)!?
1         [w] x-y: z

输出:

>>> df['Tweet'].str.replace(r'[^0-9a-zA-Z\s]+', '', regex=True)
0    abc def hij
1         w xy z

推荐阅读