我有以下格式的数据,我想使用熊猫为其创建数据透视表。
AGE Good_Bad
(30, 45] Good
(30, 45] Good
(20, 30] Bad
(60, 100] Good
(30, 45] Bad
age列是通过使用pd.cut()
对Age变量进行装箱而生成的。我的目标是使用数据透视表绘制每个年龄段的Good
和Bad
s的摘要。
AGE
Good Bad
(20, 30] 0 1
(60, 100] 1 0
(30, 45] 2 1
很明显,我的index='AGE', columns='Good_Bad', values=count of 'Good_Bad'
。但是,当我将其以以下形式输入时,会出现一个关键错误:
df_age.pivot_table(index = 'AGE', columns = 'Good_Bad', values = 'Good_Bad', aggfunc='count')
到目前为止,我已经能够使用groupby函数和Google表格来解决此问题,但是即使尝试了几件事,它也无法与df.pivot_table()一起使用。
我在做什么错了?