0

I have two dataframes, that i'll call A and B.

The DF A, has UsersId as index and 8000 rows and 9 columns like this:

index col1 col2
aaa 22 22
bbb 33 33
DEF 44 44

The DF B, also has UsersID as index and 500 rows and 4 columns, like this:

index col1 col2
ccc 22 22
ddd 33 33
DEF 44 44

My question is, how do i merge those both dataframes into a new one and than DELETE the row with the index def ?

To be more clearer, i need my newDF like this:

index col1 col2
abc 22 22
bcd 33 33
ghi 22 22
jkl 33 33

This is how i want the new df, merged but with the index DEF and all row information deleted from it. Hope someone can help me :).The index on both tables is the UserID and it has different numbers of rows and columns, but same index.

  • 3
    This is called an `ANTI-JOIN`. Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – BeRT2me Oct 01 '22 at 23:26
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 02 '22 at 02:50

1 Answers1

0

If my understanding is correct, you could simply concatenate the two dataframes as they are and then drop duplicates on index:

newDF = pd.concat([df1, df2], ignore_index=False)
newDF = newDF[~newDF.index.duplicated(keep=False)]

When concatenating the two dataframes, ignore_index = False will preserve the indices of the original dataframes, so as to be able to drop them later on!

theodosis
  • 894
  • 5
  • 15