0

I need to get the column name that has max value for specific row.

    a   b   c
0   2   2   1
1   1   5   5
2   1   1   5
3   1   1   3
4   5   4   1
5   5   2   5

I expect to get column c when I search in index=2 and to get column a when I search by index=4.

I tried this solution but it doesn't work. In my real df, when I checked using df.info() , it shows me that Dtype is object not numeric.

Maged
  • 818
  • 1
  • 8
  • 17

2 Answers2

0

I found the solution by converting the Dtype from object to float

Find the column name which has the maximum value for each row

then I applied

Find the column name which has the maximum value for each row

Maged
  • 818
  • 1
  • 8
  • 17
0

You wrote that dtype is object, but on the other hand, your data sample contains only integers and I assume that it is a representative sample.

So the first step is to change dtype to int:

df = df.astype(int)

Then use the following function:

def maxColName(df, ind):
    return df.loc[ind].idxmax()

Or you can drop df parameter and use this name as a global variable:

def maxColName(ind):
    return df.loc[ind].idxmax()
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41