[英]How to aggregate sum, and convert unique row values to column names, in pandas?
I have issue with pandas pd.groupby()
function.我有熊猫pd.groupby()
函数的问题。 I have DataFrame我有数据框
data = [{'Shop': 'Venga', 'Item Name': 'Oranges', 'Measure':'Supply Cost', 'Value': '10'},
{'Shop': 'Venga', 'Item Name': 'Oranges', 'Measure':'Product Cost', 'Value': '20'},
{'Shop': 'Venga', 'Item Name': 'Apples', 'Measure':'Supply Cost', 'Value': '5'},
{'Shop': 'Venga', 'Item Name': 'Apples', 'Measure':'Product Cost', 'Value': '60'},
{'Shop': 'Mesto', 'Item Name': 'Oranges', 'Measure':'Supply Cost', 'Value': '15'},
{'Shop': 'Mesto', 'Item Name': 'Oranges', 'Measure':'Product Cost', 'Value': '10'},
{'Shop': 'Mesto', 'Item Name': 'Apples', 'Measure':'Supply Cost', 'Value': '80'},
{'Shop': 'Mesto', 'Item Name': 'Apples', 'Measure':'Product Cost', 'Value': '5'},
]
I want to move my categories of Measure
to columns and make it look like this:我想将我的Measure
类别移到列中,并使其看起来像这样:
I have tried to run data.groupby(['Measure'], axis = 1).sum()
but it doesn't work at all for me.我试图运行data.groupby(['Measure'], axis = 1).sum()
但它对我来说根本不起作用。
.groupby
and then .unstack
the correct level.使用.groupby
然后.unstack
正确的级别。
level=2
is the 'Measure'
column, from the .groupby
object.在这种情况下, level=2
是来自.groupby
对象的'Measure'
列。.reset_index
to remove the multi-level index. .reset_index
删除多级索引。import pandas as pd
dfg = df.groupby(['Shop', 'Item Name', 'Measure'])['Value'].sum().unstack(level=2).reset_index()
dfg.columns.name = None
# display(dfg)
Shop Item Name Product Cost Supply Cost
0 Mesto Apples 5 80
1 Mesto Oranges 10 15
2 Venga Apples 60 5
3 Venga Oranges 20 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.