Python 正在从中的列中删除字符串列表

Python 正在从中的列中删除字符串列表,python,pandas,Python,Pandas,我需要删除字符串列表: list_strings=['describe','include','any'] 从pandas中的一列: My_Column include details about your goal describe expected and actual results show some code anywhere 我试过了 df['My_Column']=df['My_Column'].str.replace('|'.join(list_strings), '')

我需要删除字符串列表:

list_strings=['describe','include','any']
从pandas中的一列:

My_Column

include details about your goal
describe expected and actual results
show some code anywhere
我试过了

df['My_Column']=df['My_Column'].str.replace('|'.join(list_strings), '')
但它删除了部分单词

例如:

My_Column

details about your goal
expected and actual results
show some code where # here it should be anywhere
我的预期产出:

My_Column

details about your goal
expected and actual results
show some code anywhere 

.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, '')
取得

像这样使用“单词边界”表达式
\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

您的问题是,
pandas
看不到单词,它只看到字符列表。所以,当你要求熊猫删除“any”时,它并不是从描述单词开始的。所以一个选择是自己去做,也许像这样:

#您的数据
df=pd.DataFrame({'My_Column':
[“包括有关您目标的详细信息”,
“描述预期和实际结果”,
'在任何地方显示一些代码']})
list_strings=['descripe','include','any']#确保它是小写的
def删除_单词:
如果s不是无:
返回“”。join(如果x.lower()不在列表字符串中,则x代表s.split()中的x)
#将函数应用于列
df.My_Column=df.My_Column.map(删除单词)

如果您要求pandas在找到字符串“any”时删除该字符串,则结果是有意义的。如果您想要任何以“any”开头的单词,那么您可能需要执行一个正则表达式,如
“any[a-z]*”
我想删除列表中的单词,因此只删除any,而不删除任何地方。我是新来的,所以可能我的代码出错了哦,我明白了,我误解了。如果你正在删除不存在的单词,我会发一条帖子。描述->描述?格式字符串first@MarianD当前位置我觉得你几乎没读过这篇文章,你的回答也解决不了问题。他想删除列表中的单词,但他的问题是有些单词是其他单词的前缀,他不想删除这些单词。Hi MarianD,谢谢你的帮助。我想从我的_栏中删除列表中的单词。所以只有任何地方,没有任何地方。谢谢你用大写字母写的点评!我添加了
regex=False
参数以获得适当的结果-请参阅我编辑的答案。谢谢MarianD。不幸的是,它仍然不是working@user105599,我更正了我的答案-我希望现在一切正常。哇,我不知道这个单词边界选项,这是超级处理我得到这个错误:AttributeError:'float'对象没有属性'split',我想你的数据框中缺少值了吧?我修正了处理那个案子的密码。但不管怎样,我认为你应该选择零答案,它可能更快,更“pythonic”
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