1

Hi I have a list of keywords.

keyword_list=['one','two']

DF,

    Name      Description
    Sri       Sri is one of the good singer in this two
    Ram       Ram is one of the good cricket player

I want to find the rows which are having all the values from my keyword_list.

my desired output is,

output_Df,
    Name    Description
    Sri     Sri is one of the good singer in this two

I tried, mask=DF['Description'].str.contains() method but I can do this only for a single word pls help.

1 Answer 1

2

Use np.logical_and + reduce of all masks created by list comprehension:

keyword_list=['one','two']

m = np.logical_and.reduce([df['Description'].str.contains(x) for x in keyword_list])
df1 = df[m]
print (df1)

  Name                                Description
0  Sri  Sri is one of the good singer in this two

Alternatives for mask:

m = np.all([df['Description'].str.contains(x) for x in keyword_list], axis=0)

#if no NaNs
m = [set(x.split()) >= set(keyword_list) for x in df['Description']]
Sign up to request clarification or add additional context in comments.

3 Comments

your solution for the above question is working fine, could you please suggest me best tutorials for numpy and pandas as I want to be good with it.
Hard question, in my opinion the best are pandas documentation and tutorials, especially I like modern pandas.
I did. I was told to wait for sometime to accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.