Skip to main content
 首页 » 编程设计

python之Pandas GroupBy 两列,根据一列计算总数,但根据聚合器的总数计算百分比

2025年05月04日25zhujiabin

我已经得出了我想要的分组,但想根据每月的总数计算一个百分比列,即不管 originating_system_id 中的字符串如何

d = [('Total_RFQ_For_Month', 'size')] 
df_RFQ_Channel = df.groupby(['Year_Month','originating_system_id'])['state'].agg(d) 
#df_RFQ_Channel['RFQ_Pcent_For_Month'] = ? 
display(df_RFQ_Channel) 
 
Year_Month  originating_system_id   Total_RFQ_For_Month RFQ_Pcent_For_Month 
2017-11              BBT                      59              7.90% 
                     EUCR                     33              4.42% 
                     MAXL                     6               0.80% 
                     MXUS                     649             86.88% 
2017-12              BBT                      36              73.47% 
                     EUCR                     7               14.29% 
                     MAXL                     6               12.24% 
2018-01              BBT                      88              9.52% 
                     EUCR                     26              2.81% 
                     MAXL                     4               0.43% 
                     MXUS                     800             86.58% 
                     VOIX                     6               0.65% 

例子:

7.90% is BBT's Total_RFQ_For_Month (59) divided by the sum of all for 2017-11 (747)  
2.81% is EUCR's Total_RFQ_For_Month (26) divided by the sum of all for 2018-01 (924).  

请您参考如下方法:

使用transform对于与原始 DataFrame 大小相同的 Series,因此可以除以 Total_RFQ_For_Month 列:

#create columns from MultiIndex 
df = df.reset_index() 
 
s = df.groupby('Year_Month')['Total_RFQ_For_Month'].transform('sum') 
df['RFQ_Pcent_For_Month'] = df['Total_RFQ_For_Month'].div(s).mul(100).round(2) 
print (df) 
   Year_Month originating_system_id  Total_RFQ_For_Month  RFQ_Pcent_For_Month 
0     2017-11                   BBT                   59                 7.90 
1     2017-11                  EUCR                   33                 4.42 
2     2017-11                  MAXL                    6                 0.80 
3     2017-11                  MXUS                  649                86.88 
4     2017-12                   BBT                   36                73.47 
5     2017-12                  EUCR                    7                14.29 
6     2017-12                  MAXL                    6                12.24 
7     2018-01                   BBT                   88                 9.52 
8     2018-01                  EUCR                   26                 2.81 
9     2018-01                  MAXL                    4                 0.43 
10    2018-01                  MXUS                  800                86.58 
11    2018-01                  VOIX                    6                 0.65 

对于百分比:

df['RFQ_Pcent_For_Month'] = (df['Total_RFQ_For_Month'].div(s) 
                                                     .mul(100) 
                                                     .round(2) 
                                                     .astype(str) 
                                                     .add('%')) 
print (df) 
   Year_Month originating_system_id  Total_RFQ_For_Month RFQ_Pcent_For_Month 
0     2017-11                   BBT                   59                7.9% 
1     2017-11                  EUCR                   33               4.42% 
2     2017-11                  MAXL                    6                0.8% 
3     2017-11                  MXUS                  649              86.88% 
4     2017-12                   BBT                   36              73.47% 
5     2017-12                  EUCR                    7              14.29% 
6     2017-12                  MAXL                    6              12.24% 
7     2018-01                   BBT                   88               9.52% 
8     2018-01                  EUCR                   26               2.81% 
9     2018-01                  MAXL                    4               0.43% 
10    2018-01                  MXUS                  800              86.58% 
11    2018-01                  VOIX                    6               0.65% 

详细信息:

print (s) 
0     747 
1     747 
2     747 
3     747 
4      49 
5      49 
6      49 
7     924 
8     924 
9     924 
10    924 
11    924 
Name: Total_RFQ_For_Month, dtype: int64