python - check if string in pandas dataframe column is in list -


if have frame this

frame = pd.dataframe({'a' : ['the cat blue', 'the sky green', 'the dog black']}) 

and want check if of rows contain word have this.

frame['b'] = frame.a.str.contains("dog") | frame.a.str.contains("cat") | frame.a.str.contains("fish") 

frame['b'] outputs:

true false true 

if decide make list

mylist =['dog', 'cat', 'fish'] 

how check rows contain word in list?

the str.contains method accepts regular expression pattern:

in [11]: pattern = '|'.join(mylist)  in [12]: pattern out[12]: 'dog|cat|fish'  in [13]: frame.a.str.contains(pattern) out[13]: 0     true 1    false 2     true name: a, dtype: bool 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -