Pandas delete row if matches string (case-insensitive)

Viewed 666

I am trying to delete rows in my dataframe if it contains something from my list.

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

Current Result:

Searches
the person was Drunk
This is a test
The Owner was not in

Expected Result:

Searches
This is a test

is there a way to conduct the search but allow it to match even if it is upper cases etc? I am unable to convert it to lower and then do the search as it breaks the other functions i have in the script. Is there an easy solution for this?

2 Answers

Add case=False parameter to 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

You can convert the strings to lowercase before checking.

df = df[~df['Searches'].str.lower().str.contains('|'.join(Search_List))]
Related