python Pandas pivot on bin

df['bin'] = pd.cut(df.age, [0,4,9,14])

pvtdf = df.pivot_table(index='type', columns=['bin'], values='days', 
                       aggfunc=('count', 'sum')).fillna(0)

#       count                   sum               
# bin  (0, 4] (4, 9] (9, 14] (0, 4] (4, 9] (9, 14]
# type                                            
# a       2.0    0.0     1.0    6.0    0.0     1.0
# b       3.0    0.0     0.0    9.0    0.0     0.0
# c       0.0    1.0     0.0    0.0    1.0     0.0
# d       0.0    1.0     0.0    0.0    4.0     0.0
# e       0.0    0.0     1.0    0.0    0.0     2.0
# f       0.0    1.0     0.0    0.0    0.0     0.0

Here is what the above code is Doing:
1. Create a new column called ‘bin’ that is the result of pd.cut(df.age, [0,4,9,14])
2. Create a pivot table with index=’type’, columns=[‘bin’], values=’days’,
aggfunc=(‘count’, ‘sum’)
3. Fill any NaN values with 0