我试图从我的数据帧中的一列中删除所有非字母数字字符,但一直无法找到解决方案,直到我遇到regex.谁能给我解释一下为什么第一行代码不起作用,而第二行代码起作用了?我查看了文档(https://pandas.pydata.org/pandas-docs/stable/user_guide/text.html#),并按照我所看到的进行操作.
以下是带有输出的第一行代码:
teams_df['Team'] = (teams_df['Team'].astype('string')
.str.replace('\W', '', regex=False)
)
以下是运行正常的代码/输出行:
teams_df['Team'] = (teams_df['Team'].astype('string')
.str.replace(r'\W', '', regex=True)
)
我更喜欢使用第一行代码,但它不起作用.