JodeCharger100
JodeCharger100

Reputation: 1059

Remove rows that have common index/indices from two dataframes

I have the following sample dataframes df1 and df2:

df1:

index    forts
cherry    0.65
apple     0.85
mangoes   0.1
bananas   0.7
grapes    0.88

df2:

index    forts
cherry    0.35
peaches   0.45
mangoes   0.14
vanilla   0.57
straws    0.89

Seeing that both DataFrames have cherry and mangoes as common indices, despite having different values under the first column, I still want df1 to remove common indices from df2, and to do the same to df2, and keep them separate.

print(df1[(df1['forts']!=df2['forts'])].dropna(how='all')) 

does not work, as it looks for duplicates based on index and column value

The final df1 and df2 should look like the following:

df1:

index    forts
apple     0.85
bananas   0.7
grapes    0.88 

df2:

index    forts
peaches   0.45
vanilla   0.57
straws    0.89

Upvotes: 0

Views: 469

Answers (2)

sammywemmy
sammywemmy

Reputation: 28659

You can get the symmetric_difference between the two indices and reindex on that, and dropna :

difference = df1.index.symmetric_difference(df2.index)

#reindex and dropna :
df1 = df1.reindex(difference).dropna()
df2 = df2.reindex(difference).dropna()

Upvotes: 1

BENY
BENY

Reputation: 323226

Let us try

df1=df1[~df1.index.isin(df2.index)]
df2=df2[~df2.index.isin(df1.index)]

Upvotes: 1

Related Questions