Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max value and the corresponding column/index name in entire dataframe

I want to select the maximum value in a dataframe, and then find out the index and the column name of that value. Is there a way to do it?

Say, in the example below, I want to first find the max value (31), and then return the index and column name of that value (20, R20D)

a = pd.DataFrame({'R05D':[1,2,3],'R10D':[7,4,3],'R20D':[31,2,4]},index=[20,25,30])

Thanks!

like image 771
pome Avatar asked Nov 13 '16 01:11

pome


1 Answers

If you call a.max(axis=0) you get a series of the max on each column:

R05D     3
R10D     7
R20D    31
dtype: int64

If you call max on that series you get it's maximum so:

a.max(axis=0).max()
#31

gives you the maximum value. Similarly:

a.max(axis=0).idxmax()
#R20D

gives you the column name and

a.max(axis=1).idxmax()
#20

will give you the row.

like image 78
Ryan Walker Avatar answered Sep 25 '22 14:09

Ryan Walker