python - 从 Pandas Dataframe 中删除 puncts
问题描述
我想使用推文从数据框中删除所有标点符号和拉丁字符以进行情感分析。代码如下。我想从列中删除标点符号,但代码删除了文本,只剩下标点符号!!有什么建议么?
remove_puncts = lambda x: re.sub("[^A-Za-z0-9\s]+", "", str(x))
df['new'] = df.Tweet.apply(remove_puncts)
解决方案
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
推荐阅读
- kubernetes - Goroutine 在每个请求(sqlx)和代码后打开一个新的数据库连接
- c# - 有没有代码隐藏的控件绑定上的按钮触发 UpdateSource?
- r - 如何根据字符串变量的一部分重新组合因子水平?
- jquery - 无法将我的数据从函数中取出(Javascript 和 jQuery)
- javascript - 如何使用 javascript 在 html 表单中显示计算
- java - 如何在 JMeter 中修复 java.io.NotSerializableException: org.apache.jmeter.JMeter$ListenToTest?
- python - 循环字典,但也在 Python 中更新它
- python - SQLite 的分页
- saml - 如何从 ping federate open Token 获取信息
- ssl - Sublime 2 安装包失败,出现 SSL 异常 tlsv1alert