【发布时间】:2018-03-29 05:01:05
【问题描述】:
我有一个这样的数据框:
testdf
category item inventory sold
0 A Low 100 50
1 A High 200 75
2 A Med 130 20
3 A Misc 435 150
4 A High 130 65
5 A Misc 120 88
6 B Misc 321 230
7 B High 453 450
8 B Low 321 301
9 B Low 122 80
我根据类别和项目对其进行分组
dfindx = testdf.groupby(['category','item']).agg({'inventory':['count','sum']})
dfindx
inventory
count sum
category item
A High 2 330
Low 1 100
Med 1 130
Misc 2 555
B High 1 453
Low 2 443
Misc 1 321
现在在聚合索引表时,我遇到了这个错误:
dfindx.groupby(['category'])[['count']].sum()
KeyError:“未找到列:'count'”
dfindx.columns
MultiIndex(levels=[['inventory'], ['count', 'sum']],
labels=[[0, 0], [0, 1]])
我无法正确引用计数和总和来汇总分组表。
dfindx.groupby(['category'])[inventory['count']].sum()
NameError:名称“库存”未定义
dfindx.groupby(['category'])[['inventory']['count']].sum()
TypeError: 列表索引必须是整数或切片,而不是 str
【问题讨论】:
标签: python pandas indexing summary