如何在熊猫数据透视表中合并多索引层?

时间:2019-08-10 06:07:25

标签: python pandas merge pivot-table multi-index

比方说,我得到了像这样的比赛中球员表现的数据框:

PreferenceScreen

我想对每场比赛的球队统计数据A和B进行汇总,换句话说,获得如下数据框:

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

我知道我可以手动形成每列,但是我一直在寻找解决问题的更优雅的方法。所以,我尝试了这个:

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

哪些给了我以下内容:

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

现在,是否有任何方法可以将这些多索引合并以将其转换为“联盟A”,“部落A”,“联盟B”,“部落B”列?我唯一的想法就是申请

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

...将删除多索引层,但是,此后需要手动重命名列。

1 个答案:

答案 0 :(得分:0)

这很容易,因为您已经完成了大部分工作:

# 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')
相关问题