如何使用 groupby 计算 pandas 数据框中特定列的总数百分比?

How to work out percentage of total with groupby for specific columns in a pandas 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'

我想通过 'Name' 计算 'Profit'、'Promotion' 和 'Product' 列的总数百分比,以获得以下数据框:

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

我尝试按 'Name'、'Status' 和 'Month' 进行分组,并尝试执行与此处提供的解决方案类似的操作 Pandas percentage of total with groupby 但似乎无法获得我想要的输出。

使用 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