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.
Nakeuh
  • 1,757
  • 3
  • 26
  • 65

1 Answers1

6
df3 = df1.loc[~df1['ID'].isin(df2['ID'])].copy()
iamchoosinganame
  • 1,090
  • 6
  • 15