如何获得两个MultiIndex DataFrame的联合?

时间:2015-11-01 07:47:46

标签: python pandas

如何合并两个MultiIndexed DataFrame?

例如,让我们说:

index1 = pd.MultiIndex.from_tuples([('2010-01-01', 'Jim'),
                                    ('2010-01-01', 'Mike'), 
                                    ('2010-01-02', 'Sam')])
index2 = pd.MultiIndex.from_tuples([('2010-01-02', 'Jim'), 
                                    ('2010-01-02', 'Sam'), 
                                    ('2010-01-03', 'Joe')])
df1 = pd.DataFrame([[7,0,7],[4,3,2],[6,2,6]], 
                   index=index1, columns=['a', 'b', 'c'])
df2 = pd.DataFrame([[4,2,0],[8,8,4],[5,5,3]], 
                   index=index2, columns=['a', 'b', 'c'])

这导致:

>> df1
                  a  b  c
2010-01-01  Jim   7  0  7
            Mike  4  3  2
2010-01-02  Sam   6  2  6

>> df2
                  a  b  c
2010-01-02  Jim   4  2  0
            Sam   8  8  4
2010-01-03  Joe   5  5  3

我想合并df1df2来制作:

>> df3
                    a1  b1  c1  a2  b2  c2
2010-01-01  Jim     7   0   7   NaN NaN NaN
            Mike    4   3   2   NaN NaN NaN
2010-01-02  Jim     NaN NaN NaN 4   2   0
            Sam     6   2   6   8   8   4
2010-01-03  Joe     NaN NaN NaN 5   5   3

我很难找到一个很好的方法来做到这一点。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

尝试:

df3 = df1.join(df2, how='outer', lsuffix='1', rsuffix='2')

或者

df3 = pd.merge(df1, df2, how='outer', left_index=True, right_index=True)