The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Index Max – pd.DataFrame.idxmax()

Pandas idxmax index of maximum value

Pandas idxmax is a very convenient function that will return the index of the maximum value across a specified axis. It is especially useful when trying to figure out the max column values for your rows OR maximum row value for your columns.

Say you have a list of students as your rows, and test scores in your columns. Which test was the highest for each student? .idxmax() is a one-liner that’ll tell you.

1. pd.DataFrame.idxmax(axis='your_axis_for_max')

Pseudo code: For a given axis, tell me where the highest value is on the other axis.

Pandas idxmax

Index Max Parameters

Small tip: idxmax works best when your index is consistent. If you start using set_index() or reset_index() then your idxmax might give you back confusing results.

  • axis: Which axis would you like to find your highest value intersections for? Axis=1 means you want to find the highest column for each row. Axis=0 means you want to find the highest row for each column!
  • skipna (Default=True): Rarely used. By default pandas will skip the NA values. If you don’t want to skip them, set skipna=False.

Let’s look at a fun example of students and test scores

Link to code

Official Documentation

On this page