Find the maximum value and the corresponding column / index name in the entire data area

I want to select the maximum value in the data frame, and then find out the index and column name of this value. Is there any way to do this?

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

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

Thank!

+4
source share
2 answers

If you call a.max(axis=0), you get a series of maximum values ​​for each column:

R05D     3
R10D     7
R20D    31
dtype: int64

If you call maxin this series, you get the maximum:

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

gives the maximum value. Similarly:

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

gives the column name and

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

will give you a string.

+4

dataframe MultipleIndex Series argmax idxmax:

coord = a.stack().argmax()
coord
(20, 'R20D')

, loc:

df.loc[coord]
31
+4

Source: https://habr.com/ru/post/1660607/


All Articles