问题
Hi my data frame is as below
Date Key y
1/2/2013 A 1
1/2/2013 B 2
1/2/2013 C 1
2/2/2013 A 1
2/2/2013 c 1
2/2/2013 B 3
I now want to create a new column "ratio" which is for a given date(1/2/2013), ratio of key A would be y(A)/(y(A)+y(B)+y(C)) which is 1/(1+2+1) i.e 0.25. My final df would be as follows
Date Key y ratio
1/2/2013 A 1 0.25
1/2/2013 B 2 0.5
1/2/2013 C 1 0.25
2/2/2013 A 1 0.2
2/2/2013 c 1 0.2
2/2/2013 B 3 0.6
really appreciate the help
回答1:
You can use groupby().transform('sum')
to compute the sum of the group, then it's just a simple division:
df['ratio'] = df['y']/df.groupby('Date')['y'].transform('sum')
Output:
Date Key y ratio
0 1/2/2013 A 1 0.25
1 1/2/2013 B 2 0.50
2 1/2/2013 C 1 0.25
3 2/2/2013 A 1 0.20
4 2/2/2013 c 1 0.20
5 2/2/2013 B 3 0.60
来源:https://stackoverflow.com/questions/61594560/getting-ratio-by-iterating-over-two-columns