1

I have a dataframe like this

name event  spending
abc   A       500
abc   B       300
abc   C       200
xyz   A       2000
xyz   D       1000

So i need a groupby name and event and calculate respective percentile...so output should be like

name  event  spending_percentile
abc   A       50%
abc   B       30%
abc   C       20%
xyz   A       66.67%
xyz   D       33.33%

Please guide how to do this in pandas Dataframe.

Satya
  • 5,470
  • 17
  • 47
  • 72
  • 1
    related and probable dupe: https://stackoverflow.com/questions/39388820/pandas-groupwise-percentage – EdChum May 10 '17 at 08:42
  • @EdChum: sorry, did not google properly (was in hurry)..Thanks, that link helps. – Satya May 10 '17 at 08:46

1 Answers1

2

It seems you need transform:

df['spending_percentile'] = df['spending'].div(df.groupby('name')['spending']
                                                 .transform(sum)).mul(100)
print (df)
  name event  spending  spending_percentile
0  abc     A       500            50.000000
1  abc     B       300            30.000000
2  abc     C       200            20.000000
3  xyz     A      2000            66.666667
4  xyz     D      1000            33.333333
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252