Pandas return row with the maximum value of a column

I’m trying to figure out how to return the row of a pandas dataframe with the maximum value in a certain column.

I know that to find the maximum value in a column I use:

df['columnName'].max()

But I’m having a hard time figuring out how to return the whole row with the maximum value in a named column.

Is there a way to do this?

Hi @charlesd,

You can try the following:

df.iloc[df['column_name'].idxmax()]

idxmax() will return the index position of the row with the highest value.
Then you can use iloc to return the row with that index.

WARNING: If there are multiple max values this method will only return the first row with the max value.

1 Like

Thanks!

I’m getting a ‘Series’ object has no attribute ‘idmax’ error.

Hmm, so when I was manipulating the dataset did I change this column to a series?

I’ve checked and it says that the type of the column is ‘float64’.

It should be idxmax instead of idmax.

1 Like

Let me know if that works.

Will it be a problem only to return the first max value (if there are multiple max values)?

Nope, this will work great for my purposes. Since this is being used for outlier detection with store sales I don’t expect that we’ll see many duplicates for Maximum values unless there was some type of error.

I wasn’t expecting the output to format the columns in this particular way, but I don’t think that will be a problem.
Still getting used to pandas.
Thank you! :grinning: