Reputation: 7275
Here's my data I df
Id Food
1 Apple
1 Banana
2 Banana
2 Cherry
3 Banana
4 DragonFruit
The other dataset df2
Id
2
4
The result will be
Id Food
2 Banana
2 Cherry
4 DragonFruit
Here's my code
df2['one'] = 1
df = df.merge(df2, on='Id', how='left')
df = df[df['one'] == 1]
df= df.drop_duplicates()
df = df[['Id' , 'Food']]
It works, but I guess there's exist better method
Upvotes: 2
Views: 80
Reputation: 51425
As an alternate solution, you could continue to use merge
, without the left
argument (i.e. do an inner merge):
df.merge(df2, on='Id')
Id Food
0 2 Banana
1 2 Cherry
2 4 DragonFruit
Upvotes: 1