连接多索引数据帧

Concating multiindex dataframes

我有一个多索引数据框,外层索引由股票名称组成,内层索引由日期组成。

                     Open        High        Low         Close
AAPL    2010-01-04  213.429998  214.499996  212.380001  214.009998  
        2010-01-08  210.299994  212.000006  209.060005  211.980005

OPK     2010-01-04  213.429998  214.499996  212.380001  214.009998  
        2010-01-08  210.299994  212.000006  209.060005  211.980005

我有一个新数据框,其中一些日期未包含在原始数据框中,我想附加它们。

                       Open          High        Low         Close

AAPL    2010-01-09  219.429998  219.499996  212.380001  214.009998  
        2010-01-10  219.299994  219.000006  209.060005  211.980005

OPK     2010-01-09  219.429998  219.499996  212.380001  214.009998  
        2010-01-10  219.299994  219.000006  209.060005  211.980005

所以我正在寻找的输出是

                      Open       High        Low         Close
AAPL    2010-01-04  213.429998  214.499996  212.380001  214.009998  
        2010-01-08  210.299994  212.000006  209.060005  211.980005
        2010-01-09  219.429998  219.499996  212.380001  214.009998  
        2010-01-10  219.299994  219.000006  209.060005  211.980005

        2010-01-04  213.429998  214.499996  212.380001  214.009998  
        2010-01-08  210.299994  212.000006  209.060005  211.980005
OPK     2010-01-09  219.429998  219.499996  212.380001  214.009998  
        2010-01-10  219.299994  219.000006  209.060005  211.980005

我尝试了这些的变体

    pd.concat([stocks_df, df])
    pd.concat([stocks_df, df], levels = [stocks_df.index] )

但似乎找不到解决方案。

我认为你需要concat with sort_index:

df = pd.concat([stocks_df, df]).sort_index()
print (df)
                       Open        High         Low       Close
AAPL 2010-01-04  213.429998  214.499996  212.380001  214.009998
     2010-01-08  210.299994  212.000006  209.060005  211.980005
     2010-01-09  219.429998  219.499996  212.380001  214.009998
     2010-01-10  219.299994  219.000006  209.060005  211.980005
OPK  2010-01-04  213.429998  214.499996  212.380001  214.009998
     2010-01-08  210.299994  212.000006  209.060005  211.980005
     2010-01-09  219.429998  219.499996  212.380001  214.009998
     2010-01-10  219.299994  219.000006  209.060005  211.980005