0

I have a list of words like this: list1 = ["I","have","a","headache"]

and a pandas data frame like this:

enter image description here

I want to search word by word in the "list1" with the words in "keyWord" column and get the "id".

I would appreciate help and/or any pointers

I want to search the words in the list with the data frame. not to search the data frame with the list.

output must be like:

keyword id

headache 1

sithara
  • 97
  • 1
  • 10

1 Answers1

1

Option 1: isin with boolean indexing

df[df['keyWord'].isin(list1)]
    keyWord  id
0  headache   1

Option 2: df.reindex:

df.set_index('keyWord').reindex(list1).dropna()
           id
keyWord      
headache  1.0

Note that python and pandas are case sensitive, so headache is different from Headache.

Peter Leimbigler
  • 10,775
  • 1
  • 23
  • 37