2024-03-28 12:42:09 发布
网友
我在从数据帧中删除停止字时遇到问题。我的代码是这样的:
for word in stopwords: df['name'] = df['name'].str.replace(word, '')
我得到一个错误:sre\u constants.error:在位置0处没有要重复的内容。 有没有解决这个错误的方法,或者有没有其他方法来替换停止字
用regex=True试试df.replace:
regex=True
df.replace
例如:
import pandas as pd stopwords = ["AAA", "BBB"] df = pd.DataFrame({"name": ["Hello", "World", "AAA", "BBB"]}) print( df["name"].replace("|".join(stopwords), "", regex=True))
输出:
0 Hello 1 World 2 3 Name: name, dtype: object
用
regex=True
试试df.replace
:例如:
输出:
相关问题 更多 >
编程相关推荐