cvl*_*pke 5 python regex replace special-characters pandas
我无法从 pandas 数据框中删除所有特殊字符。你能帮我吗?
我尝试过这样的事情:
df = df.replace(r'\W+', '', regex=True)
Run Code Online (Sandbox Code Playgroud)
因为我在最近的一篇文章中发现了它。但是当我执行时,特殊字符“'”不会消失。
我知道在 PostgresSQL 中有类似[^\w]获取特定列表的功能。python中有类似的东西可以做类似的事情吗
a) 只保留字母
b) 只保留数字
c) 保留字母和数字
感谢您的帮助!
[^0-9a-zA-Z ]匹配 Unicode 字母和数字,这会删除太多内容。
使用
df = df.replace(r'[^\w\s]|_', '', regex=True)
Run Code Online (Sandbox Code Playgroud)
查看证明
解释
--------------------------------------------------------------------------------
[^\w\s] any character except word characters (\p{L}, \p{N}, _)
and whitespace (\p{Z})
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
_ '_'
Run Code Online (Sandbox Code Playgroud)