I have a dataframe:
col1 col2 a 0 b 1 c 1 d 0 c 1 d 0
'col2' I want to keep only the first
1 from the top and replace every
1 below the first one with a
0, such that the output is:
col1 col2 a 0 b 1 c 0 d 0 c 0 d 0
Thank you very much.
You can find the index of the first
1 and set others to
mask = df['col2'].eq(1) df.loc[mask & (df.index != mask.idxmax()), 'col2'] = 0
For better performance, see Efficiently return the index of the first value satisfying condition in array.