Pandas dataframe: Remove secondary upcoming same value

  • A+

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.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: