Pandas 数据帧多索引合并 [英] Pandas Dataframe Multiindex Merge
本文介绍了Pandas 数据帧多索引合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问一个关于在 Pandas 中合并多索引数据框的问题,这里是一个假设场景:
I wanted to ask a questions regarding merging multiindex dataframe in pandas, here is a hypothetical scenario:
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()
才能使其工作?
Do I have to do reset_index()
on either s1
/s2
to make this work?
推荐答案
看来你需要结合使用它们.
Seems like you need to use a combination of them.
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
这篇关于Pandas 数据帧多索引合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文