b3dog
b3dog

Reputation: 125

Dropping rows with same values from another dataframe

I have one dataframe (df) with a column called "id". I have another dataframe (df2) with only one column called "id". I want to drop the rows in df that have the same values in "id" as df2.

How would I go about doing this?

Upvotes: 1

Views: 39

Answers (1)

piRSquared
piRSquared

Reputation: 294198

use boolean indexing with the isin method.

Note that the tilde ~ indicates that I take the negation of the boolean series returned by df['id'].isin(df2['id'])

df[~df['id'].isin(df2['id'])]

query

Using a query string we refer df2 using the @ symbol.

df.query('id not in @df2.id')

Upvotes: 2

Related Questions