0

I have a df

df = pd.DataFrame({'group':['a','a','b','b','c','c','c'], 'score':[1,2,3,4,5,6,7]})

group score
0   a   1
1   a   2
2   b   3
3   b   4
4   c   5
5   c   6
6   c   7

I want to divide each row by the sum of its group. So for group a, each of the 2 rows will divide by 3 (2+1), for group c, each of the 3 rows will divide by 18 (5+6+7), giving:

group score
0   a   0.33
1   a   0.67
2   b   0.43
3   b   0.57
4   c   0.28
5   c   0.33
6   c   0.39

I was thinking of using a df.groupby('group')['score'].apply(lambda x: x / sum???) approach but can't figure it out. Any help will be appreciated!

TYL
  • 1,577
  • 20
  • 33

0 Answers0