Satya
Satya

Reputation: 5907

groupby and percentile calculation in pandas dataframe

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.

Upvotes: 1

Views: 934

Answers (1)

jezrael
jezrael

Reputation: 862511

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

Upvotes: 2

Related Questions