简体   繁体   English

如何计算 pandas dataframe 中特定列的 groupby 百分比?

[英]How to work out percentage of total with groupby for specific columns in a pandas dataframe?

I have the following dataframe:我有以下 dataframe:

df = pd.DataFrame( columns = ['Name','Status','Profit','Promotion','Product','Visits']) 
df['Name'] = ['Andy','Andy','Brad','Brad','Cynthia','Cynthia']
df['Status'] =['Old','New','Old','New','Old','New'] 
df['Profit'] = [140,60,110,90,20,100]
df['Promotion'] = [25,30,40,10,22,36]
df['Product'] = [8,6,18,10,7,12]
df['Visits'] = [11,4,7,3,12,5]
df['Month'] = 'Jan'

I would like to work out the percentage of total for the columns 'Profit','Promotion' and 'Product' by 'Name' in order to achieve the following dataframe:我想按“名称”计算“利润”、“促销”和“产品”列的总百分比,以实现以下 dataframe:

df['Profit'] = [70,30,55,45,17,83]
df['Promotion'] = [45,55,80,20,38,62]
df['Product'] = [57,43,64,36,37,63]
df

I have attempted to group by 'Name','Status' and 'Month' and tried doing something similar to the solution provided here Pandas percentage of total with groupby but can't seem to get my desired output.我尝试按“名称”、“状态”和“月份”进行分组,并尝试执行类似于此处提供的解决方案Pandas 与 groupby 的总百分比,但似乎无法获得我想要的 output。

Use GroupBy.transform for sum per Name s with divide original columns, multiple by 100 and last round :GroupBy.transform用于每个Name的总和,将原始列除以 100 和最后round

cols = ['Profit','Promotion','Product']

print (df.groupby('Name')[cols].transform('sum'))
   Profit  Promotion  Product
0     200         55       14
1     200         55       14
2     200         50       28
3     200         50       28
4     120         58       19
5     120         58       19

df[cols] = df[cols].div(df.groupby('Name')[cols].transform('sum')).mul(100).round()
print (df)
      Name Status  Profit  Promotion  Product  Visits Month
0     Andy    Old    70.0       45.0     57.0      11   Jan
1     Andy    New    30.0       55.0     43.0       4   Jan
2     Brad    Old    55.0       80.0     64.0       7   Jan
3     Brad    New    45.0       20.0     36.0       3   Jan
4  Cynthia    Old    17.0       38.0     37.0      12   Jan
5  Cynthia    New    83.0       62.0     63.0       5   Jan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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