1

I have a pandas dataframe like this

enter image description here

and I have a list of keywords

branches_of_sci = ['bio', 'chem', 'physics']

So What I want to do is create a new column and add new keywords with a separation of coma or anything else

I was able to do this temporary

for tok in branches_of_sci:
    df[branch] = df.astype(str).sum(axis=1).str.contains(branch)

enter image description here

but it generates new column for every keyword, Can I just create a new single column where all keywords found are stored.

The marked duplicate answer only finds a single keyword but what if multiple keywords match then it fails.

df['Col 3'] = df['Col 2'].str.extract("(" + "|".join(List1) +")", expand=False)
Abhay
  • 359
  • 1
  • 2
  • 11
  • Can you explain more what failed? Can you add [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) ? – jezrael Apr 10 '18 at 06:49
  • Also I think this should help - [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – jezrael Apr 10 '18 at 06:50
  • 1
    I get it, changed dupe link. Need `findall` – jezrael Apr 10 '18 at 06:55

0 Answers0