我有一个如下的数据框:
data =[['A','ABC001','18M01',1,3],['A','ABC002','18M01',2,4],['A','ABC001','18M02',3,3],['B','ABC001','18M01',4,3],['B','ABC002','18M02',5,4],['B','ABC002','18M02',6,4]]
df = pd.DataFrame(data,columns=['Type','Product','Month','Requirement','Inventory'])
df
输入:
Type Product Month Requirement Inventory
A ABC001 18M01 1 3
A ABC002 18M01 2 4
A ABC001 18M02 3 3
B ABC001 18M01 4 3
B ABC002 18M02 5 4
B ABC002 18M02 6 4
我想做的是将它总结成一个像这样的数据框
输出:
Type Product Values 18M01 18M02
A ABC001 Sum of Requirement 1 3
A ABC001 Average of Inventory 3 3
A ABC002 Sum of Requirement 2 NaN
A ABC002 Average of Inventory 4 NaN
B ABC001 Sum of Requirement 4 NaN
B ABC001 Average of Inventory 3 NaN
B ABC002 Sum of Requirement NaN 11
B ABC002 Average of Inventory NaN 4
我可以很容易地在 pivot excel 中创建它,但是在使用 pandas pivot 时完全不知道。请帮忙
守着一只汪
白板的微信
相关分类