Home > Mobile >  finding last column name when two columns have max value in one row python
finding last column name when two columns have max value in one row python

Time:12-31

This is my data frame

  x y z

0 1 0 0

1 1 0 1

2 0 0 1

3 0 1 1

4 1 1 0

When i am using df.idxmax(axis=1)

The Output i am getting is

x

x

z

y

x                        

My desired output is

x

z

z

z

y

Please help me in finding the column name of max row when two rows contains max value

CodePudding user response:

what about taking vertical symmetry and then idxmax, i.e.,

df.iloc[:, ::-1].idxmax(axis=1)
0    x
1    z
2    z
3    z
4    y

so the df.iloc[:, ::-1] part is

   z  y  x
0  0  0  1
1  1  0  1
2  1  0  0
3  1  1  0
4  0  1  1

it's from z to x now



Another way with reindexing might be more clear in giving away the intent:

df.reindex(columns=df.columns[::-1]).idxmax(axis=1)
0    x
1    z
2    z
3    z
4    y

as above.

CodePudding user response:

why not use a map or apply or transform and call a function to produce the desired results per row?

  • Related