如果匹配字符串,熊猫删除行(不区分大小写)

时间:2021-01-20 11:48:39

标签: python pandas dataframe

如果我的数据框中包含列表中的某些内容,我将尝试删除该行。

DF

Search_List = ['drunk', 'owner']
df = df[~df['Searches'].str.contains('|'.join(Search_List))]

Searches
the person was Drunk
the owner was not available
This is a test
The Owner was not in

当前结果:

Searches
the person was Drunk
This is a test
The Owner was not in

预期结果:

Searches
This is a test

有没有办法进行搜索但允许它匹配,即使它是大写等?我无法将其转换为较低的值,然后进行搜索,因为它破坏了我在脚本中的其他功能。有没有简单的解决方案?

2 个答案:

答案 0 :(得分:5)

case=False 参数添加到 Series.str.contains

Search_List = ['drunk', 'owner']
df = df[~df['Searches'].str.contains('|'.join(Search_List), case=False)]
print (df)
         Searches
2  This is a test

答案 1 :(得分:0)

您可以在检查之前将字符串转换为 lowercase

df = df[~df['Searches'].str.lower().str.contains('|'.join(Search_List))]
相关问题