Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – Calculate success rate of column based on another column label

I have one dataframe df:

    route   value
1     1       1
2     2       2
3     2       2
4     2       1
5     1       2

The column value gives success (2) or fail (1), I want to create a third column which gives the succes rate for each route.

Desired Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    route   value   Rate
1     1       1      0.5
2     2       2      0.66
3     2       2      0.66
4     2       1      0.66
5     1       2      0.5

Thank you for your help

>Solution :

Let try transform with groupby

df['Rate'] = df['value'].eq(2).groupby(df['route']).transform('mean')
df
Out[611]: 
   route  value      Rate
1      1      1  0.500000
2      2      2  0.666667
3      2      2  0.666667
4      2      1  0.666667
5      1      2  0.500000
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading