简体   繁体   中英

How to concatenate pandas dataframes with multiple level columns

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.

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)
              

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM