使用 TomAugspurger 概述的相同测试数据
import pandas as pd
import numpy as np
# create a test data set
arrays1 = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
arrays2 = [['bar', 'baz', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'one', 'two', 'three', 'one', 'two', 'one', 'three']]
tuples1 = zip(*arrays1)
tuples2 = zip(*arrays2)
index1 = pd.MultiIndex.from_tuples(tuples1, names=['first', 'second'])
index2 = pd.MultiIndex.from_tuples(tuples2, names=['first', 'second'])
df1 = pd.DataFrame(np.random.randn(8, 2), index=index1)
df2 = pd.DataFrame(np.random.randn(8, 2), index=index2)
产生以下两个表
0 1
first second
bar one -0.579214 0.261575
two 0.912683 -0.475463
baz one -0.295739 -0.586646
two 0.031916 0.199812
foo one -0.724781 -1.245275
two -0.824759 2.270161
qux one 0.638533 0.537306
two -0.988444 -1.076636
和
0 1
first second
bar one -0.859494 0.214814
baz one -0.446976 1.281912
two -0.181159 0.574126
three 0.212799 -1.592317
foo one -1.192866 1.544799
two 1.025816 0.921364
qux one -0.927700 -0.516720
three 0.610065 0.028249
然后你可以得到不相交的数据框
df1[~df1.index.isin(df2.index)].append(df2[~df2.index.isin(df1.index)])
导致
0 1
first second
bar two 0.912683 -0.475463
qux two -0.988444 -1.076636
baz three 0.212799 -1.592317
qux three 0.610065 0.028249
那是你要求的吗?