Pandas 用于在每个级别显示 aggfunc 的函数

Pandas function for showing aggfunc at every level

假设我有一个如下所示的枢轴 table:

pd.pivot_table(
  data, 
  columns=['A','B','C'],
  values='widgets',
  aggfunc='count'
).T
[Column] Count
A  B  C  1
      D  2
   E  F  3
      G  4
H  I  J  5
   K  L  6

我想要的是:

A  10  B  3  C  1
             D  2
       E  7  F  3
             G  4
H  11  I  11 J  5
             L  6

每个类别的中间总和介于最终计数之间。

确保索引级别命名为:

df = pd.DataFrame(
    {'Count': [1, 2, 3, 4, 5, 6]},
    pd.MultiIndex.from_tuples([
        ('A', 'B', 'C'),
        ('A', 'B', 'D'),
        ('A', 'E', 'F'),
        ('A', 'E', 'G'),
        ('H', 'I', 'J'),
        ('H', 'K', 'L')
    ], names=['One', 'Two', 'Three'])
)

df

               Count
One Two Three       
A   B   C          1
        D          2
    E   F          3
        G          4
H   I   J          5
    K   L          6

from functools import reduce
import pandas as pd

names = df.index.names
reduce(
    pd.DataFrame.join,
    [df.groupby(level=names[:i+1]).sum().add_suffix(f'_{names[i]}')
     for i in range(df.index.nlevels)]
)

               Count_One  Count_Two  Count_Three
One Two Three                                   
A   B   C             10          3            1
        D             10          3            2
    E   F             10          7            3
        G             10          7            4
H   I   J             11          5            5
    K   L             11          6            6