附加两个多索引的pandas数据帧

E.K*_*.K. 6 python join multi-index pandas

你能帮忙附上两个多索引的熊猫数据帧吗?试图将df_future附加到df_current.COMPANY和DATE是索引.

df_current

                           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
Run Code Online (Sandbox Code Playgroud)

df_future

                            VALUE
COMPANY     DATE            
A           8/1/2015        5
            8/2/2015        6
B           8/1/2015        15
            8/2/2015        16
Run Code Online (Sandbox Code Playgroud)

基于这些dfs,想看看..

df_current_and_future

                            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
Run Code Online (Sandbox Code Playgroud)

unu*_*tbu 6

使用concat来连接两个DataFrames,并sort_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
Run Code Online (Sandbox Code Playgroud)

注意:我使用的原始答案sortlevel现已弃用.正如firelynx所示,请sort_index改用.