3 回答
TA贡献1852条经验 获得超7个赞
使用“词边界”\b之类的表达方式。
In [46]: df.My_Column.str.replace(r'\b{}\b'.format('|'.join(list_strings)), '')
Out[46]:
0 details about your goal
1 expected and actual results
2 show some code anywhere
Name: My_Column, dtype: object
TA贡献1817条经验 获得超6个赞
您的问题是pandas看不到单词,它只看到字符列表。因此,当你要求 pandas 删除“any”时,它并不是从描绘单词开始的。所以一种选择是你自己做,也许是这样的:
# Your data
df = pd.DataFrame({'My_Column':
['Include details about your goal',
'Describe expected and actual results',
'Show some code anywhere']})
list_strings=['describe','include','any'] # make sure it's lower case
def remove_words(s):
if s is not None:
return ' '.join(x for x in s.split() if x.lower() not in list_strings)
# Apply the function to your column
df.My_Column = df.My_Column.map(remove_words)
TA贡献1780条经验 获得超1个赞
方法的第一个参数.str.replace()必须是字符串或编译后的正则表达式;不是像你这样的列表。
你可能想要
list_strings=['Describe','Include','any'] # Note capital D and capital I
for s in [f"\\b{s}\\b" for s in list_strings]: # surrounded word boundaries (\b)
df['My_Column'] = df['My_Column'].str.replace(s, '')
获得
My_Column
0 details about your goal
1 expected and actual results
2 Show some code anywhere
添加回答
举报