简体   繁体   English

如何在熊猫中汇总总和并将唯一的行值转换为列名?

[英]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()但它对我来说根本不起作用。

  • Use .groupby and then .unstack the correct level.使用.groupby然后.unstack正确的级别。
    • In this case, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM