Jithesh Erancheri
Jithesh Erancheri

Reputation: 305

How to remove rows from a dataframe based on another

I have been try my level best to compare two data frames in a specific manner but not successful. I hope experts over here can help with solution.

Below is my problem description: I have two dataframes.

Data frame #1 looks like this.

df1:
pid  name  age
121  John   36
132  Mary   26
132   Jim   46
145   Kim   50

Dataframe#2 looks like below

df2:
pid  name  age
121  John   32
132   Tom   28
132 Susan   40
155   Kim   50

I want to compare both df's in such a way that those rows in df2 which don't have the same pid's in df1 should be deleted.

My new data frame #2 should look like below

df2:
pid  name  age
121  John   32
132   Tom   28
132 Susan   40

Highly appreciate your help on this.

Upvotes: 3

Views: 70

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

You could use isin as in

df2[df2.pid.isin(df1.pid)]

which will return only the rows of df2 whose pid is in df1.

Upvotes: 4

Related Questions