Home > Software engineering >  How to concatenate pandas dataframes with multiple level columns
How to concatenate pandas dataframes with multiple level columns

Time:02-15

There are several DataFrames like this shape.

data = {'AAA': DataFrame, 'BBB': DataFrame, ...}

Each of DataFrame is like below.

Date        A  B  C  D
2022-02-14  1  5  2  9
...
2022-02-14  2  5  8  7

I want to concatenate them like this.

ATTR          A         B         C         D
SYMBOL      AAA  BBB  AAA  BBB  AAA  BBB  AAA  BBB
Date
2022-02-14    1    2    5    9    2    4    9    2
...
2022-02-14    2    6    5    3    8    6    7    3

When I used pandas 1.3.5, I did this.

data = pd.concat(data, sort=True).unstack(level=0)
data.columns.names = ['ATTR', 'SYMBOL']

But, in pandas 1.4.0, I don't know how to do it.

Please help me out. I wasted many hours on this.

CodePudding user response:

I think here is problem duplicated DatetimeIndex, so unstack failed. Possible solution is add counter level to index by GroupBy.cumcount and DataFrame.set_index, reshape by DataFrame.unstack and lasr remove helper level by DataFrame.droplevel:

data = pd.concat(data, sort=True)

data = pd.concat(data, sort=True)
data = (data.set_index(data.groupby(level=[0,1]).cumcount(), append=True)
            .unstack(level=0)
            .droplevel(-1))
print (data)
              
  • Related