Home > database >  Adding a rows corresponding column to a new column [duplicate]
Adding a rows corresponding column to a new column [duplicate]

Time:09-21

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

CodePudding user response:

Sounds like a job for idxmax.

test['max'] = test.idxmax(axis='columns')

CodePudding user response:

Try this:

test['max'] = test.T.idxmax()
  • Related