我需要将列名更改为“ Count”,而不是age_cat。另外,是否有一种方法可以在单个分组呼叫中同时进行计数和比例计算?我的代码如下:
agecat = voter.groupby('age_cat').aggregate({'age_cat':'count'})
agecat['Proportion'] = round(agecat['age_cat']*100/sum(agecat.age_cat),2)
输出:
age_cat Proportion
age_cat
0-10 222 0.05
10-20 14508 3.34
20-30 80457 18.52
30-40 87798 20.21
40-50 72287 16.64
50-60 66279 15.26
60-70 56917 13.10
70-80 33227 7.65
80-90 17463 4.02
90+ 5259 1.21