0

I have a table constructed like so, I find with these types of normalized data it can be hard to quickly see which column has the highest value:

Test dataframe

The code is test = pd.DataFrame({'0 to 4': [0.031, 0.23, 0.13], '5 to 9':[0.32, 0.142, 0.532], '10 to 14': [0.24, 0.131, 0.564]}, index=['Barking and Dagenham', 'Barnet', 'Bexley'])

What I am trying to do is find the maximum value of each row and put the corresponding column name into a new column. Like so: Result dataframe

I've tried lambda and some other methods but I'm a bit stumped right now, any ideas?

Thanks

Elliot.L
  • 1
  • 1
  • It's better to paste the text representation of a dataframe into your question than an image of the notebook graphic. – Bill Sep 20 '21 at 16:17

2 Answers2

1

Sounds like a job for idxmax.

test['max'] = test.idxmax(axis='columns')
Imre Kerr
  • 2,388
  • 14
  • 34
0

Try this:

test['max'] = test.T.idxmax()
Bill
  • 10,323
  • 10
  • 62
  • 85