Reputation: 397
i want to create new column that will calculate a ratio groupby date and count like shown below in the result of my new column new.
date count place moy new
2021-07-04 396 NY 30. 30/(30+10+20+40)
2021-07-04 396 OX 10. 10/(30+10+20+40)
2021-07-04 396 CA 20 20/(30+10+20+40)
2021-07-04 396 LA 40 40/(30+10+20+40)
2021-07-05 592 NY 30. 30/(30+50+10+30)
2021-07-05 592 OX 50. 50/120
2021-07-05 592 NY 10. 10/120
2021-07-05 592 OX 30. 30/12O
Upvotes: 0
Views: 56
Reputation: 24324
Try via groupby()
and transform()
:
df['new']=df['moy']/df.groupby(['date','count'])['moy'].transform('sum')
#OR
df['new']=df['moy'].div(df.groupby(['date','count'])['moy'].transform('sum'))
If needed you can round the values by using round()
method:
df['new']=df['new'].round(2)
Now If you print df
you will get your desired output
Upvotes: 1