0

My input dataframe: df1

line_item_product_code  cost Account     
AWSCloudTrail            10  AU-MGT   
AWSGlue                  12  AU-MGT  
AWSQueueService          15  AU-MGT   
AWSSecretsManager        20  AU-PRD  
AmazonDynamoDB           30  AU-PRD

I want to calculate the percentage based on specific account

Output dataframe:

line_item_product_code  cost Account     percentage
AWSCloudTrail            10  AU-MGT      27.02 
AWSGlue                  12  AU-MGT      32.43
AWSQueueService          15  AU-MGT      40.54
AWSSecretsManager        20  AU-PRD      40
AmazonDynamoDB           30  AU-PRD      60

How can I achieve this in pandas? Example Calculation for AU-MGT and AWSCloudTrail:

percentage = 10*100/(10+12+15)
Dcook
  • 899
  • 7
  • 32

1 Answers1

3

A simple groupby + transform should do the trick:

>>> df['percentage'] = df['cost'] / df.groupby('Account')['cost'].transform('sum') * 100
>>> df
  line_item_product_code  cost Account  percentage
0          AWSCloudTrail    10  AU-MGT   27.027027
1                AWSGlue    12  AU-MGT   32.432432
2        AWSQueueService    15  AU-MGT   40.540541
3      AWSSecretsManager    20  AU-PRD   40.000000
4         AmazonDynamoDB    30  AU-PRD   60.000000
Cimbali
  • 11,012
  • 1
  • 39
  • 68
  • [This related question](https://stackoverflow.com/questions/23377108/pandas-percentage-of-total-with-groupby) addresses the efficiency of different methods to calculate percentages per group. – mozway Jul 07 '21 at 17:17