0

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

Rabinzel
  • 7,757
  • 3
  • 10
  • 30
Movilla
  • 166
  • 8

1 Answers1

2

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
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks ! I noticed that if I want to group by 2 routes (like route 1 & route2), `df['Rate'] = df['value'].eq(2).groupby(df[['route1','route2']]).transform('mean')` does not work : `Grouper for '' not 1-dimensional`, why is that ? – Movilla May 16 '22 at 14:59
  • ok I got the answer here my bad : https://stackoverflow.com/questions/27517425/apply-vs-transform-on-a-group-object – Movilla May 16 '22 at 15:02