5

I have two Pandas Dataframe df1 and df2 where df2 is a part of df1 and I want to create a Dataframe df3, which contains all the rows from df1 that are not in df2.

Here is an example :

print(df1)

>>
+---------+
|       ID|
+---------+
|      AAA|
|      DDD|
|      BBB|
|      CCC|
|      EEE|
|      FFF|
+---------+

print(df2)

>>
+---------+
|       ID|
+---------+
|      AAA|
|      EEE|
|      FFF|
+---------+

print(df3)

>>
+---------+
|       ID|
+---------+
|      DDD|
|      BBB|
|      CCC|
+---------+

Note:

  • My DataFrame might have multiple columns, but the matching must be done on the ID column only.
0

1 Answer 1

6
df3 = df1.loc[~df1['ID'].isin(df2['ID'])].copy()
Sign up to request clarification or add additional context in comments.

1 Comment

Can you add a little detail to what the above like what the code do?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.