简体   繁体   中英

Pandas multiple filter str.contains or not contains

I need to build multiple filter on 2 columns structure of table is 7 columns , but first 'query' and last 'template' is filtering

I done it beforeand it worked but now (1 year later) i cant figure out whats wrong.

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")) ]

Expected result is the table without rows containing strings - egg*, . phospho , vipe in column 'query' AND rows in column 'template' which contain 'ABC1' or 'bender'.

I think there's something with the missing brackets in your condition.

Try this:

table[(
       # AND condition
       table['query'].str.contains("egg*", regex=True)==False &
       table['query'].str.contains(".*phospho*", regex=True)==False &
       table['query'].str.contains("vipe", regex=True)==False &
       # OR condition
       (table['template'].str.contains("ABC1") |
        table['template'].str.contains("bender"))
      )]

My answer for problem:

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")) ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM