Python 试图替换pandas数据帧中的停止字,sre_constants.error发生

Python 试图替换pandas数据帧中的停止字,sre_constants.error发生,python,pandas,Python,Pandas,我在从熊猫数据帧中删除停止字时遇到问题。我的代码是这样的: for word in stopwords: df['name'] = df['name'].str.replace(word, '') 我得到一个错误:sre_常量。错误:在位置0没有要重复的内容。 是否有任何解决错误的方法,或任何其他方法来替换停止字请尝试df。将替换为regex=True: Ex: import pandas as pd stopwords = ["AAA", "BBB"] df = pd.DataFram

我在从熊猫数据帧中删除停止字时遇到问题。我的代码是这样的:

for word in stopwords: 
  df['name'] = df['name'].str.replace(word, '')
我得到一个错误:sre_常量。错误:在位置0没有要重复的内容。
是否有任何解决错误的方法,或任何其他方法来替换停止字

请尝试
df。将
替换为
regex=True

Ex:

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
输出:

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

这很有帮助。谢谢