Replace MAX value with 1 (else = 0) rowwise in Pandas DF

I have a DF with a lot of colums, how can I replace the values with either (1 or 0) given that the MAX value per row gets a 1 and all others 0. So if I have

v1 v2 v3 1  3  2 0  4  .5 .1 .1 .7 

And get:

v1 v2 v3 0  1  0 0  1  0 0  0  1    

Numbers are floats, given previous calculations NO duplicate numbers per row are possible.

Thanks,

Add Comment
1 Answer(s)

We can do max then eq

df=df.eq(df.max(axis=1),axis=0).astype(int)    v1  v2  v3 0   0   1   0 1   0   1   0 2   0   0   1 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.