Home > Back-end >  How to change column values based on value of an index
How to change column values based on value of an index

Time:08-10

                                                                S/R
Batfast_id   Session_no   Event_Name   Overs  Deliveries_faced
bfs_1        1            fulham       0      5                  20
                                       1      11                 45
                                       2      17                 30
bfs_2        1            gps          0      5                  55
                                       1      11                 34
                                       2      17                 27

I have a df as above with a multi index of Batfast_id, Session_no, Event_Name, Overs, Deliveries_faced. I want to make the S/R = 0, everywhere Deliveries_faced is 5.

CodePudding user response:

Try this:

import numpy as np
df['S/R'] = np.where(df['Deliveries_faced']==5, 0, df['S/R'])

This solution is not affected by index columns

  • Related