Unknown Coder
Unknown Coder

Reputation: 6731

Pandas str.contains over a list?

I use something like the following to update my dataframe

df.loc[(df['Message'].str.contains('hello', case=False)),'SomeSeries'] = 'SomeUpdate'

But I might want to update when my series contains 'hello' or 'bicycle' or 'monday', etc. Obviously, I could iterate over a list but I'm wondering if there is a way to do that in a single line? I would like something like

watchlist = ['hello','bicycle','monday']
df.loc[(df['Message'].str.contains(watchlist, case=False)),'SomeSeries'] = 'SomeUpdate'

Upvotes: 3

Views: 2377

Answers (1)

Pyd
Pyd

Reputation: 6159

you need,

 df.loc[(df['Message'].str.contains('|'.join(watchlist), case=False)),'SomeSeries'] = 'SomeUpdate'

Upvotes: 5

Related Questions