1df['bin'] = pd.cut(df.age, [0,4,9,14])
2
3pvtdf = df.pivot_table(index='type', columns=['bin'], values='days',
4 aggfunc=('count', 'sum')).fillna(0)
5
6# count sum
7# bin (0, 4] (4, 9] (9, 14] (0, 4] (4, 9] (9, 14]
8# type
9# a 2.0 0.0 1.0 6.0 0.0 1.0
10# b 3.0 0.0 0.0 9.0 0.0 0.0
11# c 0.0 1.0 0.0 0.0 1.0 0.0
12# d 0.0 1.0 0.0 0.0 4.0 0.0
13# e 0.0 0.0 1.0 0.0 0.0 2.0
14# f 0.0 1.0 0.0 0.0 0.0 0.0
15