Pandas : vectorized operations on maximum values per row

Viewed 1284

I have the following pandas dataframe df:

index        A    B    C
    1        1    2    3
    2        9    5    4
    3        7    12   8
    ...      ...  ...  ...

I want the maximum value of each row to remain unchanged, and all the other values to become -1. The output would thus look like this :

index        A    B    C
    1       -1   -1    3
    2        9   -1   -1
    3       -1    12  -1
    ...      ...  ...  ...

By using df.max(axis = 1), I get a pandas Series with the maximum values per row. However, I'm not sure how to use these maximums optimally to create the result I need. I'm looking for a vectorized, fast implementation.

3 Answers
Related