0

I'm struggling with next task: I would like to identify using pandas (or any other tool on python) if any of multiple cells (Fruit 1 through Fruit 3) in each row from Table 2 contains in column Fruits of Table1. And at the end obtain "Contains Fruits Table 2?" table.

Fruits
apple
orange
grape
melon
Name Fruit 1 Fruit 2 Fruit 3 Contains Fruits Table 2?
Mike apple Yes
Bob peach pear orange Yes
Jack banana No
Rob peach banana No
Rita apple orange banana Yes

Fruits in Table 2 can be up to 40 columns. Number of rows in Table1 is about 300.

I hope it is understandable, and someone can help me resolve this.

I really appreciate the support in advance!

1 Answer 1

1

Try:

  1. filter DataFrame to include columns that contain the word "Fruit"
  2. Use isin to check if the values are in table1["Fruits"]
  3. Return True if any of fruits are found
  4. map True/False to "Yes"/"No"
table2["Contains Fruits Table 2"] = table2.filter(like="Fruit")
                                          .isin(table1["Fruits"].tolist())
                                          .any(axis=1)
                                          .map({True: "Yes", False: "No"})

>>> table2

   Name Fruit 1 Fruit 2 Fruit 3 Contains Fruits Table 2
0  Mike   apple    None    None                     Yes
1   Bob   peach    pear  orange                     Yes
2  Jack  banana    None    None                      No
3   Rob   peach  banana    None                      No
4  Rita   apple  orange  banana                     Yes
​~~~
Sign up to request clarification or add additional context in comments.

4 Comments

You don't need apply, DataFrames have also an 'isin' method. You can do .isin(table1['Fruits'].to_numpy()) instead.
Upvoted, but to_numpy() or tolist() is required, otherwise the match will be done by index alignment ;)
Correct again @HarryPlotter :)
You are awesome! Thank you very much!

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.