get columns from key words + count occurences

I have a problem to solve. I need to create new columns from given key_words list and sum their occurrence in data frame.

key_words = [‘apple’, ‘animal’, ‘everyone’]

input data frame:

id description xx
1 Apple is a healthy fruit. Everyone should eat it. ..
2 Lion is a denagerous animal. ..
3 Everyone likes him. ..

what I want to get:

id description xx apple animal everyone
1 Apple is a healthy fruit. Everyone should eat it. .. 1 0 1
2 Lion is a dangerous animal. .. 0 1 0
3 Everyone likes him. .. 0 0 1

any help much appreciated.

>Solution :

This will work for you

key_words = ['apple', 'animal', 'everyone']
for key in key_words:
  df[key] = df['description'].str.lower().str.count(key)

Leave a Reply