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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With