【问题标题】:Append two multiindexed pandas dataframes附加两个多索引熊猫数据框
【发布时间】:2015-10-22 05:10:06
【问题描述】:

您能帮忙附加两个多索引熊猫数据框吗?试图将 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

df_future

                            VALUE
COMPANY     DATE            
A           8/1/2015        5
            8/2/2015        6
B           8/1/2015        15
            8/2/2015        16

根据这些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

【问题讨论】:

    标签: python join pandas multi-index


    【解决方案1】:

    使用concat 连接两个DataFrame,使用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
    

    注意:我的原始答案使用了 sortlevel,现在已弃用。作为firelynx shows,请改用sort_index。

    【讨论】:

    • unutbu 先生,你太棒了!非常感谢!
    【解决方案2】:

    在 pandas 中追加称为 concat。并用the pd.concat function.完成

    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() 部分。谢谢!
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    相关资源
    最近更新 更多