How To Filter Dataframe Rows If Column Value (string) Contains Any Of The Values In A Set In Python?
I want to filter rows if cell string contains anyone of the values in the predefined set. For example, for following dataframe: ids ids2 vals 0 a h a i 1 1 b z n a
Solution 1:
Use case = False
to make it case-insensitive:
d[d['ids'].str.contains('h', case=False)|d['ids'].str.contains('i',case=False)]
This is definitely a little roundabout but it will work:
letters = ['h', 'i']
d[d['ids'].str.split().apply(lambda x: len(set(x).intersection(set(letters))))>0]
Solution 2:
You can do this easily by passing a regex that joins the terms:
In[132]:
d[~d['ids'].str.contains('h|i', case=False)]
Out[132]:
idsids2vals1bzna22fzca3
Post a Comment for "How To Filter Dataframe Rows If Column Value (string) Contains Any Of The Values In A Set In Python?"