如果匹配字符串,熊猫删除行(不区分大小写)
如果我的数据框中包含列表中的某些内容,我会尝试删除它。
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
有没有办法进行搜索但允许它匹配,即使它是大写等?我无法将其转换为较低,然后进行搜索,因为它破坏了我在脚本中的其他功能。有没有一个简单的解决方案?
回答
将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