Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading