getting ratio by iterating over two columns

此生再无相见时 提交于 2021-02-17 07:04:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!