我想问一个关于在熊猫中合并多索引数据帧的问题,这里是一个假设场景:
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index1 = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
index2 = pd.MultiIndex.from_tuples(tuples, names=['third', 'fourth'])
s1 = pd.DataFrame(np.random.randn(8), index=index1, columns=['s1'])
s2 = pd.DataFrame(np.random.randn(8), index=index2, columns=['s2'])
那么要么
s1.merge(s2, how='left', left_index=True, right_index=True)
或
s1.merge(s2, how='left', left_on=['first', 'second'], right_on=['third', 'fourth'])
会导致错误。
我是否必须在s1/s2上执行reset_index()以使其正常工作?
谢谢
最佳答案
似乎你需要结合使用它们。
s1.merge(s2, left_index=True, right_on=['third', 'fourth'])
#s1.merge(s2, right_index=True, left_on=['first', 'second'])
输出:
s1 s2
bar one 0.765385 -0.365508
two 1.462860 0.751862
baz one 0.304163 0.761663
two -0.816658 -1.810634
foo one 1.891434 1.450081
two 0.571294 1.116862
qux one 1.056516 -0.052927
two -0.574916 -1.197596
关于python - Pandas Dataframe Multiindex Merge,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52785579/