Home > Software design >  Pandas - Calculate success rate of column based on another column label
Pandas - Calculate success rate of column based on another column label

Time:05-17

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:

    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

CodePudding user response:

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
  • Related