Home > Mobile >  How to find indexes of a dataframe on the basis of a list of words?
How to find indexes of a dataframe on the basis of a list of words?

Time:10-15

I have a dataframe and a list:


b = pd.DataFrame({'text': ["like","reduce","carbon","emissions","reduce carbon emissions"],'score': [1,-1,-1,-1,1]})

a = ['like', 'reduce carbon emissions']

I would like to find the indexes where the words in aare in b so that I can sum score accordingly.

I am pretty sure this has been asked many times but I can't find any reference.

Can anyone help me?

Thanks!

CodePudding user response:

Try with isin

b.loc[b.text.isin(a)]
Out[45]: 
                      text  score
0                     like      1
4  reduce carbon emissions      1
  • Related