如何对多个列上的数据集进行分组并同时进行不同的汇总?蟒蛇

时间:2020-10-16 13:55:51

标签: python pandas group-by aggregate-functions

我需要完成几件事:

  • 按国家和地区分组的列

  • 执行聚合以获取:

     - percentage of my Products column for each country
     - Calculate the sum of columns Volume and Profit and UnrealizedProfit (2 columns 1st=Volume, 2nd= Profit + UnrealizedProfit)
    
  • 也显示其他列

我的数据框:

   Country       Sector       Products     Volume    Profit   UnrealizedProfit      
0  Country_1     Sector1      Product_1     50         5            4
1  Country_1     Sector2      Product_2     100        6            3
2  Country_2     Sector1      Product_1     150        3            -1
3  Country_2     Sector2      Product_2     200        -1           5
4  Country_1     Sector1      Product_2     100        7            10
5  Country_2     Sector2      Product_2     200        -3           -1
6  Country_2     Sector1      Product_1     150        2            -1
7  Country_1     Sector2      Product_1     50         5            -3

注意:我的实际数据框中有几千行。

所需的输出如下所示:

   Country       Sector       Products     Product%   Volume    ExpectedProfit        
0  Country_1     Sector1      Product_1     0.138      100        11
1  Country_1     Sector2      Product_2     0.861      200        26
2  Country_2     Sector1      Product_1     0.667      300        3
3  Country_2     Sector2      Product_2     0.333      400        0

我一次只能进行一次汇总,但不能两次。 到目前为止:

df = (data1.groupby('Country')['Products']
           .value_counts(normalize=True,sort=False)
           .reset_index(name='Product%'))
print (df)

这段代码给我:

   Country       Products     Product%
0  Country 1     Product 1     0.138
1  Country 1     Product 2     0.861
2  Country 2     Product 1     0.667
3  Country 2     Product 2     0.333

产品的每个频率均基于相关国家-> sum(Country1)= 100%,sum(Country2)= 100%...

对于该卷,我设法将其复制:

df = (data1.groupby(['Country','Product'])['Volume']
      .sum()
      .reset_index(name='Volume'))

我将产品添加到groupby()中是因为我想查看每种产品和国家/地区的数量。

目标是合并产品百分比和数量并添加ExpectedProfit 我不知道如何将其合并并进行利润汇总,如前所述(Profit + UnrealizedProfit) 并显示该部门(我猜该部门可能包含在Groupby()中,因为每个部门都有多个产品。

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

所有总结:

new_df = pd.DataFrame()
grouper = df.groupby(["Country", "Products"])
new_df["ExpectedProfit"] = grouper.Profit.sum() + grouper.UnrealizedProfit.sum()
new_df["Volume"] =  grouper.Volume.sum()
new_df["%"] = df.groupby(['Country']).Products.value_counts(normalize=True,sort=False)
new_df

输出:

                     ExpectedProfit  Volume    %
Country   Products                              
Country_1 Product_1              11     100  0.5
          Product_2              26     200  0.5
Country_2 Product_1               3     300  0.5
          Product_2               0     400  0.5

有部门

grouper = df.groupby(["Country", "Products", "Sector"])

无扇区:

grouper = df.groupby(["Country", "Products"])
result = grouper.Profit.sum() + grouper.UnrealizedProfit.sum()
result = result.reset_index(name="ExpectedProfit")

结果部门:

     Country   Products   Sector  ExpectedProfit
0  Country_1  Product_1  Sector1               9
1  Country_1  Product_1  Sector2               2
2  Country_1  Product_2  Sector1              17
3  Country_1  Product_2  Sector2               9
4  Country_2  Product_1  Sector1               3
5  Country_2  Product_2  Sector2               0

无结果:

     Country   Products  ExpectedProfit
0  Country_1  Product_1              11
1  Country_1  Product_2              26
2  Country_2  Product_1               3
3  Country_2  Product_2               0
相关问题