0

I have a DataFrame like such:

id, fruit
1, apple
1, kiwi
2, melon
2, kiwi
3, mango
3, melon
3, mango

I have a Pandas Series like such:

id, fruit
1, apple
2, kiwi
3, mango

I want to filter my DataFrame such that for each row, its 'fruit' matches the corresponding value in the Series. For id=1, since fruit in series is apple, it filters only rows with fruit apple. For id=2, since fruit in series is kiwi, it filters only rows with fruit kiwi. For id=3, since fruit in series is mango, it filters only rows with fruit mango. I want:

id, fruit
1, apple
2, kiwi
3, mango
3, mango

Any suggestions?

*edit because it was incorrectly marked as an existing question.

Jake Lee
  • 1
  • 2
  • @cᴏʟᴅsᴘᴇᴇᴅ hey, I think you incorrectly marked my question as an existing question. The question you linked to is not a solution to my question. – Jake Lee Apr 23 '18 at 15:44
  • @COLDSPEED please – Jake Lee Apr 23 '18 at 15:48
  • Are you sure? `df[df.fruit.isin(df2.fruit.tolist())]` – cs95 Apr 23 '18 at 15:49
  • yes, I edited my dataframe to make my example more specific: added (1, kiwi). Here, (1, kiwi) is not populated in the filtered dataframe because the key-value pair of id=1 is (id=1,fruit=apple) as specified in the series. sorry, my initial example was not specific enough. – Jake Lee Apr 23 '18 at 15:54
  • So both the column _and_ id need to match? – cs95 Apr 23 '18 at 15:55
  • In that case, do something like `df.merge(df2)` – cs95 Apr 23 '18 at 15:56

0 Answers0