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'])
Then either
s1.merge(s2, how='left', left_index=True, right_index=True)
or
s1.merge(s2, how='left', left_on=['first', 'second'], right_on=['third', 'fourth'])
will result in error.
Do I have to do reset_index() on either s1/s2 to make this work?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…