Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从两个数据帧中删除具有公共索引的行_Python_Python 3.x_Pandas - Fatal编程技术网

Python 从两个数据帧中删除具有公共索引的行

Python 从两个数据帧中删除具有公共索引的行,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有以下示例数据帧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 看到两个数据帧都有cherry和mangoes作为公共索引,尽管第一列下的值不同

我有以下示例数据帧
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
看到两个数据帧都有cherry和mangoes作为公共索引,尽管第一列下的值不同,我仍然希望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
让我们试试

df1=df1[~df1.index.isin(df2.index)]
df2=df2[~df2.index.isin(df1.index)]
让我们试试

df1=df1[~df1.index.isin(df2.index)]
df2=df2[~df2.index.isin(df1.index)]
您可以获得两个索引之间的值,并在此基础上重新索引,以及:

您可以获得两个索引之间的值,并在此基础上重新索引,以及: