I have a dataframe:
col1 col2
a 0
b 1
c 1
d 0
c 1
d 0
On '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 0
:
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.
np.flatnonzero
Because I thought we needed more answers
df.loc[df.index[np.flatnonzero(df.col2)[1:]], 'col2'] -= 1
df
col1 col2
0 a 0
1 b 1
2 c 0
3 d 0
4 c 0
5 d 0
Same thing but a little more sneaky.
df.col2.values[np.flatnonzero(df.col2.values)[1:]] -= 1
df
col1 col2
0 a 0
1 b 1
2 c 0
3 d 0
4 c 0
5 d 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With