Michael
Michael

Reputation: 351

Filter dataframe for words which do not contain any of the letters in a list

I'm looking for a way to filter a dataframe on a list of letters. I wish for my output dataframe to be the words from the original dataframe which do not contain any of the letters in the list

For instance

letter_list = ['a','d','o','m','s']

>>>df

ID     WORD
1     'yellow'
2     'orange'
3     'green'
4     'blue'
5     'red'

>>> expected output

ID     WORD
3     'green'
4     'blue'

I'm not sure how to go about this

Thanks for any help

Upvotes: 2

Views: 529

Answers (1)

jezrael
jezrael

Reputation: 863301

Use Series.str.contains with joined letters with | for regex or for filter by matched values and add ~ for filter by inverse mask, so get not matched rows:

df = df[~df['WORD'].str.contains('|'.join(letter_list))]
print (df)
   ID     WORD
2   3  'green'
3   4   'blue'

Upvotes: 3

Related Questions