Home > Mobile >  how to change dict of dataframes into one dataframe with multicolumns
how to change dict of dataframes into one dataframe with multicolumns

Time:06-01

As I got several items like:

item = pd.DataFrame({'item1': np.random.randn(3),
                     'item2': np.random.randn(3)},
                    index=['a', 'b', 'c'])

And then save them into one dict:

product = {'product1': item,
           'product2': item}

Now I want to change this dict into one dataframe, then I think in the form of multi columns is a better choice. But how could I change it into this kind of result?

             product1           product2
      item1     item2    item1     item2
a  2.517220 -0.391607 2.517220 -0.391607
b  0.546790  1.533278 0.546790  1.533278
c  1.187944  0.981451 1.187944  0.981451

CodePudding user response:

Use pd.concat

pd.concat({'product1': item,
           'product2': item}, axis=1)
#pd.concat(product, axis=1)
  • Related