简体   繁体   中英

How to merge multi-index layers in pandas pivot-table?

Let's say I got the dataframe for players perfomance in matches like this:

    Match    Faction    A         B
    BG1      Alliance   8         10
    BG1      Alliance   2         5
    BG1      Horde      5         25
    BG2 ...

I want to aggregate teams stats A and B per match, in other words, get dataframe like this:

    Match  Alliance A  Alliance B  Horde A  Horde B
    BG1    10          15          5        25
    BG2 ...

I know I can just form each columns manually, but I was looking for more elegant way to solve the problem. So, I tried this:

    df.pivot_table(values=['A', 'B'], index='Match', columns='Faction', aggfunc=lambda x: x.sum())

Which gives me the following:

             A                B
    Faction  Alliance  Horde  Alliance  Horde
    Match  
    BG1      10        5      15        25  
    BG2 ...

Now, is there any way to merge these multi-indexes to turn them into 'Alliance A', 'Horde A', 'Alliance B', 'Horde B' columns? My only idea was to apply

    .T.reset_index().T

...which drops multi-index layers, however, it requires manually renaming the columns after.

That's easy, because you already did most of the work:

# create a list of the new column names in the right order
new_cols=[('{1} {0}'.format(*tup)) for tup in pivoted.columns]

# assign it to the dataframe (assuming you named it pivoted
pivoted.columns= new_cols

# resort the index, so you get the columns in the order you specified
pivoted.sort_index(axis='columns')

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