首页 > 解决方案 > Pandas 多个过滤器 str.contains 或不包含

问题描述

我需要在表的 2 列结构上构建多个过滤器是 7 列,但第一个“查询”和最后一个“模板”正在过滤

我之前做过,但现在(1年后)我不知道出了什么问题。

for item in glob.glob('D:\\path\\*.change'):
    table = pd.read_csv(item, sep='\t', index_col=None)
#FILTERING
    filtered_table = table[
        (table['query'].str.contains("egg*", regex=True)==False) &
        (table['query'].str.contains(".*phospho*", regex=True)==False) &
        (table['query'].str.contains("vipe", regex=True)==False) &
        (table['template'].str.contains("ABC1")) |
        (table['template'].str.contains("bender")) ]

预期结果是没有包含字符串的行的表 - egg*, 。“查询”列中的phospho , vipe 和“模板”列中包含“ABC1”或“bender”的行。

标签: pythonpandas

解决方案


我对问题的回答:

for item in glob.glob('D:\\path\\*.change'):
    table = pd.read_csv(item, sep='\t', index_col=None)
#FILTERING
    query_table = table[
        (table['query'].str.contains("egg*", regex=True)==False) &
        (table['query'].str.contains(".*phospho*", regex=True)==False) &
        (table['query'].str.contains("vipe", regex=True)==False)  ]

  filtered_table = query_table[
        (query_table['template'].str.contains("ABC1")) |
        (query_table['template'].str.contains("bender")) ]

推荐阅读