Python: How to turn a dictionary of Dataframes into one big dataframe with column names being the key of the previous dict?

前端 未结 3 1421
心在旅途
心在旅途 2021-02-04 02:19

So my dataframe is made from lots of individual excel files, each with the the date as their file name and the prices of the fruits on that day in the spreadsheet, so the spread

3条回答
  •  天涯浪人
    2021-02-04 02:40

    You can try first set_index of all dataframes in comprehension and then use concat with remove last level of multiindex in columns:

     print d
    {'17012016':     Fruit  Price
    0  Orange      7
    1   Apple      8
    2    Pear      9, '16012016':     Fruit  Price
    0  Orange      4
    1   Apple      5
    2    Pear      6, '15012016':     Fruit  Price
    0  Orange      1
    1   Apple      2
    2    Pear      3}
    d = { k: v.set_index('Fruit') for k, v in d.items()}
    
    df = pd.concat(d, axis=1)
    df.columns = df.columns.droplevel(-1) 
    print df
            15012016  16012016  17012016
    Fruit                               
    Orange         1         4         7
    Apple          2         5         8
    Pear           3         6         9
    

提交回复
热议问题