Hello I have a dataframe such as :
COL1 COL2 COL3
A 30 400
A 32 400
A 70 400
B 32 700
B 10 700
And I would like for each COL1, to calculate the sum of COL2 / COL3.unique()
Example = 30+32+70/400 = 0.33
Here I should then get:
COL1 COL2 COL3 New_col
A 30 400 0.33
A 32 400 0.33
A 70 400 0.33
B 32 700 0.06
B 10 700 0.06
Does someone have an idea please ?
>Solution :
In your case do groupby with transform
df['new'] = df.COL3.rdiv(df.groupby('COL1')['COL2'].transform('sum'))
df
Out[19]:
COL1 COL2 COL3 new
0 A 30 400 0.33
1 A 32 400 0.33
2 A 70 400 0.33
3 B 32 700 0.06
4 B 10 700 0.06