Find maximum value of a column and return the corresponding row values using Pandas


Structure of data;

Using Python Pandas I am trying to find the 'Country' & 'Place' with the maximum value.

This returns the maximum value:


But how do I get the corresponding 'Country' and 'Place' name?

4/1/2013 10:38:03 AM

Accepted Answer

Assuming df has a unique index, this gives the row with the maximum value:

In [34]: df.loc[df['Value'].idxmax()]
Country        US
Place      Kansas
Value         894
Name: 7

Note that idxmax returns index labels. So if the DataFrame has duplicates in the index, the label may not uniquely identify the row, so df.loc may return more than one row.

Therefore, if df does not have a unique index, you must make the index unique before proceeding as above. Depending on the DataFrame, sometimes you can use stack or set_index to make the index unique. Or, you can simply reset the index (so the rows become renumbered, starting at 0):

df = df.reset_index()
5/11/2018 9:01:06 PM


This will return the entire row with max value

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow