How to search words (in a list) in pandas data frame' column?

3963 views python-3.x
1

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

answered question

1 Answer

12

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.

posted this

Have an answer?

JD

Please login first before posting an answer.