Search code examples
pandasconcatenationmulti-index

Concating multiindex dataframes


I have a multi-index dataframe, with the outer-layer index consisting of stocks names, and the innerlayer index consisting of dates.

                     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

I'm have a new dataframe with some dates not included in the original dataframe, and I want to append them.

                       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

So the output I'm looking for is

                      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

I tried variations of these

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

but can't seem to find a solution.


Solution

  • I think you need 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