📜  python Pandas pivot on bin - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:11.355000             🧑  作者: Mango

代码示例1
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