我有以下几列(仅感兴趣的列)。
Ship Mode : Product Category : Product Sub-Category : Sales
Express Air : Office : Paper : 500
Express Air : Technology : Paper : 200
Express Air : Office : Pen : 500
Regular Air : Office : Art Pen : 200
Regular Air : Technology : Art Pen : 200
Regular Air : Technology : Paper : 200
如何使用数据透视表以使其产生如下所示的结果
Ship Mode Product Sub-Category Total Sales(for each category)
Express Air - Paper 700
- Pen 500
Regular Air - Art Pen 400
- Paper 200
我使用了此语句,但是显然它不起作用。 “销售”列似乎与索引无关,因为它显示了其他各个列的总价值。
a1.pivot_table(index = ['Ship Mode','Product Sub-Category'], columns = 'Sales',aggfunc=sum )
答案 0 :(得分:1)
尝试在“销售”列中添加“ []”。
a1_pivot = a1.pivot_table(索引= ['运输方式','产品子类别'],列= ['销售'],aggfunc = sum
如果它不起作用,请尝试使用“值”而不是“列”。
a1.pivot_table(index = ['Ship Mode','Product Sub-Category'],values = ['Sales'],aggfunc = sum