Apply multiple string containment filters to pandas dataframe using dictionary

Aran Freel

I need to set a filter on multiple columns based on string containment which will be specified in the dict column_filters while ignoring text case using toupper() or something along those lines ... for example

column_filters = {'COLUMN_1': ['drum', 'gui'], 'COLUMN_2': ['sta', 'kic']}

df = pd.DataFrame({'COLUMN_1': ['DrumSet', 'GUITAR', 'String', 'Bass', 'Violin'],
                   'COLUMN_2': ['STAND', 'DO', 'KICKSET', 'CAT', 'CELLO'],
                   'COLUMN_3': ['LOSER', 'LOVE', 'LICKING', 'STICK', 'BOLOGNA'])

DataFrame to filter based On COLUMN_FILTERS dict:

         COLUMN_1   COLUMN_2    COLUMN_3
      0 DrumSet      STAND       LOSER
      1 GUITAR       DO          LOVE
      2 String       KICKSET     LICKING
      3 Bass         CAT         STICK
      4 Violin       CELLO       BOLOGNA

Result:

    COLUMN_1    COLUMN_2     COLUMN_3
0   DrumSet      STAND       LOSER
1   GUITAR       DO          LOVE
2   String       KICKSET     LICKING
EdChum

I'd convert the dict values into a regex pattern by joining all strings with '|', you can then use str.contains to filter the df:

In [50]:
for k in column_filters.keys():
    column_filters[k] = '|'.join(column_filters[k])
column_filters

Out[50]:
{'COLUMN_1': 'drum|gui', 'COLUMN_2': 'sta|kic'}

now filter using using str.contains with param case=False:

In [51]:
df.loc[(df['COLUMN_1'].str.contains(column_filters['COLUMN_1'], case=False)) | (df['COLUMN_2'].str.contains(column_filters['COLUMN_2'], case=False))]

Out[51]:
  COLUMN_1 COLUMN_2
0  DrumSet    STAND
1   GUITAR       DO
2   String  KICKSET

Update

OK there is a dynamic method:

In [68]:
df[df.apply(lambda x: x.str.contains('|'.join(column_filters[x.name]), case=False)).any(axis=1)]

Out[68]:
  COLUMN_1 COLUMN_2
0  DrumSet    STAND
1   GUITAR       DO
2   String  KICKSET

We can see without the boolean masking that it correctly matches:

In [69]:
df.apply(lambda x: x.str.contains('|'.join(column_filters[x.name]), case=False))

Out[69]:
  COLUMN_1 COLUMN_2
0     True     True
1     True    False
2    False     True
3    False    False
4    False    False

Update 2

To answer you modified question again:

In [75]:
df[df[list(column_filters.keys())].apply(lambda x: x.str.contains('|'.join(column_filters[x.name]), case=False)).any(axis=1)]

Out[75]:
  COLUMN_1 COLUMN_2 COLUMN_3
0  DrumSet    STAND    LOSER
1   GUITAR       DO     LOVE
2   String  KICKSET  LICKING

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

React: apply multiple filters to array

From Dev

Apply multiple filters to an image

From Dev

python pandas convert dataframe to dictionary with multiple values

From Dev

Pandas how to apply multiple functions to dataframe

From Dev

pandas apply function that returns multiple values to rows in pandas dataframe

From Dev

Apply string.format() to row in Pandas DataFrame

From Dev

Apply multiple css filters dynamically

From Dev

pandas.DataFrame.apply() using index as an arg

From Dev

How to filter pandas dataframe on multiple columns based on a dictionary?

From Dev

Query with multiple filters on Pandas

From Dev

How to apply function to multiple pandas dataframe

From Dev

Using ViewState to apply filters on Listview

From Dev

How can I return multiple rows from a python function to a pandas dataframe using apply?

From Dev

Can I apply a function to multiple columns in Pandas dataframe without a for loop?

From Dev

pandas apply User defined function to grouped dataframe on multiple columns

From Dev

Pandas rolling apply using multiple columns

From Dev

Pandas DataFrame.groupby() to dictionary with multiple columns for value

From Dev

How to apply string methods to multiple columns of a dataframe

From Dev

How to apply multiple custom functions on multiple columns in grouped DataFrame in pandas?

From Dev

How can I return multiple rows from a python function to a pandas dataframe using apply?

From Dev

Pandas how to apply multiple functions to dataframe

From Dev

expand each row to multiple rows in pandas using dataframe.apply (similar to MapReduce)

From Dev

Using pandas DataFrame.apply for column operations

From Dev

How to apply different audio filters in multiple audio tracks using ffmpeg?

From Dev

Using aliases with multiple filters

From Dev

pandas dataframe add multiple rows for group of values with apply

From Dev

Print dictionary to file using pandas DataFrame, but changing dataframe format

From Dev

remapping multiple column values with multiple dictionary in dataframe using python pandas

From Dev

Replace keywords in dataframe column using pandas dictionary

Related Related

  1. 1

    React: apply multiple filters to array

  2. 2

    Apply multiple filters to an image

  3. 3

    python pandas convert dataframe to dictionary with multiple values

  4. 4

    Pandas how to apply multiple functions to dataframe

  5. 5

    pandas apply function that returns multiple values to rows in pandas dataframe

  6. 6

    Apply string.format() to row in Pandas DataFrame

  7. 7

    Apply multiple css filters dynamically

  8. 8

    pandas.DataFrame.apply() using index as an arg

  9. 9

    How to filter pandas dataframe on multiple columns based on a dictionary?

  10. 10

    Query with multiple filters on Pandas

  11. 11

    How to apply function to multiple pandas dataframe

  12. 12

    Using ViewState to apply filters on Listview

  13. 13

    How can I return multiple rows from a python function to a pandas dataframe using apply?

  14. 14

    Can I apply a function to multiple columns in Pandas dataframe without a for loop?

  15. 15

    pandas apply User defined function to grouped dataframe on multiple columns

  16. 16

    Pandas rolling apply using multiple columns

  17. 17

    Pandas DataFrame.groupby() to dictionary with multiple columns for value

  18. 18

    How to apply string methods to multiple columns of a dataframe

  19. 19

    How to apply multiple custom functions on multiple columns in grouped DataFrame in pandas?

  20. 20

    How can I return multiple rows from a python function to a pandas dataframe using apply?

  21. 21

    Pandas how to apply multiple functions to dataframe

  22. 22

    expand each row to multiple rows in pandas using dataframe.apply (similar to MapReduce)

  23. 23

    Using pandas DataFrame.apply for column operations

  24. 24

    How to apply different audio filters in multiple audio tracks using ffmpeg?

  25. 25

    Using aliases with multiple filters

  26. 26

    pandas dataframe add multiple rows for group of values with apply

  27. 27

    Print dictionary to file using pandas DataFrame, but changing dataframe format

  28. 28

    remapping multiple column values with multiple dictionary in dataframe using python pandas

  29. 29

    Replace keywords in dataframe column using pandas dictionary

HotTag

Archive