我想问一个关于在大熊猫中合并多索引数据帧的问题,这是一个假设的场景:
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'])
Run Code Online (Sandbox Code Playgroud)
然后
s1.merge(s2, how='left', left_index=True, right_index=True)
Run Code Online (Sandbox Code Playgroud)
要么
s1.merge(s2, how='left', left_on=['first', 'second'], right_on=['third', 'fourth'])
Run Code Online (Sandbox Code Playgroud)
会导致错误.
我是否必须在s1/s2上执行reset_index()才能使其工作?
谢谢