python - 删除 pandas 系列中仅是标点符号的单词

标签 python regex pandas

假设我有以下 pandas 系列:

tmp = pd.Series(['k.; mlm', '(+).', 'a;b/c', '!".: abc', 'abc dfg', 'qwert@'])

我想,对于所有元素,使用正则表达式删除标点符号的单词,我试图使用类似的东西:

tmp.str.replace(regex, '')

我的最终系列将是:

tmp = pd.Series(['k.; mlm', '', 'a;b/c', 'abc', 'abc dfg', 'qwert@'])

编辑:我正在考虑使用 unicode 表标点符号

最佳答案

你可以使用 str.contains使用模式 [^\W] 来匹配至少包含一个不是标点符号的字符的字符串。

请注意,[] 匹配集合中包含的任何字符,通过在开头添加 ^,将匹配所有不在集合中的字符。

tmp.where(tmp.str.contains(r'[^\W]'), '')

0     k.; mlm
1            
2       a;b/c
3    !".: abc
4     abc dfg
5      qwert@
dtype: object

关于python - 删除 pandas 系列中仅是标点符号的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61468634/

相关文章:

c# 正则表达式模式

两个标签之间的 JavaScript 正则表达式多行文本

python - Pandas 正确列出列表中的值

python - 当我在 pandas 绘图中使用样式时,为什么不应用 colomap?

python - 如何防止 Django Web 应用程序中的服务器泛洪?

python - 自动为每个文件夹创建数据框

python - 在python中将一维数组转换为二维矩阵

java - 如果 Java 中的字符串包含数字,则拆分该字符串 - 但在结果中包含数字?

python - 错误: 'NoneType' object is not iterable when read_sql

php - Amazon EC2 上通过 php 的 Python 脚本