简体   繁体   中英

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

Let us try

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM