python - 如果匹配字符串(不区分大小写),Pandas 删除行

标签 python pandas dataframe

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

东风

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

关于python - 如果匹配字符串(不区分大小写),Pandas 删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65808963/

相关文章:

python - 替换列中的值

python - 如果数据框行为 1,其余值设置为 0,如何设置最大值

python - 查找二维列表中特定列的长度

python - 如何在 Pandas 中将嵌套列表转换为 DataFrame

python-3.x - 如何使用 pandas 中的条件从两个 SQLite 表创建 Excel 文件?

python - pandas 多个分隔符不起作用

python - 如何过滤数据框并更新特定单元格的选择性行

python - Python 中的循环 : not expecting this output

python - 通过 numpy.copy 复制字典。现在是ndarray;如何恢复原来的字典?

dataframe - pyspark 相当于 pandas groupby + apply on column