我有以下示例数据帧df1
和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
尽管第一列下的值不同,但看到两个DataFrame都具有樱桃和芒果作为公共索引,我仍然希望df1从df2中删除公共索引,并对df2进行相同操作,并将它们分开。
print(df1[(df1['forts']!=df2['forts'])].dropna(how='all'))
不起作用,因为它根据索引和列值查找重复项
最终的df1和df2应该如下所示:
df1:
index forts
apple 0.85
bananas 0.7
grapes 0.88
df2:
index forts
peaches 0.45
vanilla 0.57
straws 0.89
答案 0 :(得分:1)
让我们尝试
df1=df1[~df1.index.isin(df2.index)]
df2=df2[~df2.index.isin(df1.index)]
答案 1 :(得分:1)
您可以在两个索引之间获取symmetric_difference并对其重新索引,然后dropna:
difference = df1.index.symmetric_difference(df2.index)
#reindex and dropna :
df1 = df1.reindex(difference).dropna()
df2 = df2.reindex(difference).dropna()