Python 附加两个多索引数据帧

Python 附加两个多索引数据帧,python,join,pandas,multi-index,Python,Join,Pandas,Multi Index,您能帮我附加两个多索引数据帧吗?正在尝试将df_future附加到df_current。公司和日期是索引 df_电流 VALUE COMPANY DATE 7/27/2015 1 A 7/28/2015 2 7/29/2015 3 7/30/2015 4

您能帮我附加两个多索引数据帧吗?正在尝试将df_future附加到df_current。公司和日期是索引

df_电流

                           VALUE
COMPANY     DATE            
            7/27/2015       1
A           7/28/2015       2
            7/29/2015       3
            7/30/2015       4
            7/27/2015       11
B           7/28/2015       12
            7/29/2015       13
            7/30/2015       14
未来

                            VALUE
COMPANY     DATE            
A           8/1/2015        5
            8/2/2015        6
B           8/1/2015        15
            8/2/2015        16
基于这些dfs,希望看到

df_当前和未来

                            VALUE
COMPANY     DATE            
            7/27/2015       1
            7/28/2015       2
A           7/29/2015       3
            7/30/2015       4
            8/1/2015        5
            8/2/2015        6
            7/27/2015       11
            7/28/2015       12
B           7/29/2015       13
            7/30/2015       14
            8/1/2015        15
            8/2/2015        16

使用
concat
连接两个数据帧,并使用
sort\u index
对第一个索引级别进行重新排序:

In [167]: pd.concat([df_current, df_future]).sort_index()
Out[167]: 
                   VALUE
COMPANY DATE            
A       7/27/2015      1
        7/27/2015     11
        7/28/2015      2
        7/29/2015      3
        7/30/2015      4
        8/1/2015       5
        8/2/2015       6
B       7/28/2015     12
        7/29/2015     13
        7/30/2015     14
        8/1/2015      15
        8/2/2015      16

注意:我的原始答案使用了
sortlevel
,现在已被弃用。如图所示,改用排序索引。

在pandas中追加称为concat。就这样结束了

无论是否有多索引,concat函数都可以工作

df = pd.concat([df_current, future])

                   VALUE
COMPANY DATE            
A       7/27/2015      1
        7/28/2015      2
        7/29/2015      3
        7/30/2015      4
        7/27/2015     11
B       7/28/2015     12
        7/29/2015     13
        7/30/2015     14
A       8/1/2015       5
        8/2/2015       6
B       8/1/2015      15
        8/2/2015      16
如果排序有问题,只需使用:

df.sort_index()

                   VALUE
COMPANY DATE            
A       7/27/2015      1
        7/27/2015     11
        7/28/2015      2
        7/29/2015      3
        7/30/2015      4
        8/1/2015       5
        8/2/2015       6
B       7/28/2015     12
        7/29/2015     13
        7/30/2015     14
        8/1/2015      15
        8/2/2015      16

乌努布先生,你真了不起!非常感谢你!不知道df.sort_index()部分。非常感谢。