Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Module

df.idxmax

Returns the label of the first maximum in each column or row of a DataFrame. Missing values are skipped by default, and the axis, missing-value handling, and numeric-column selection are configurable.

How it works

Call idxmax on a DataFrame to obtain labels rather than maximum values. By default it examines each column, while axis="columns" examines each row; missing values are skipped unless configured otherwise, and ties resolve to the first maximum.

Syntax

DataFrame.idxmax(axis=0, skipna=True, numeric_only=False)

Parameters

NameTypeDescription
axis{0 or 'index', 1 or 'columns'}, default 0Axis to evaluate: 0 or "index" evaluates down rows for each column; 1 or "columns" evaluates across columns for each row. Defaults to 0.
skipnabool, default TrueWhether to omit NA/null values. Defaults to True.
numeric_onlybool, default FalseWhether to include only float, integer, or Boolean columns. Defaults to False.

Returns

Series — indexes of maxima along the specified axis.

Key points

  • The method returns the label of the first maximum encountered along the selected axis.
  • Missing values are ignored unless skipna is disabled.
  • Use axis 0/index for column-wise aggregation and axis 1/columns for row-wise aggregation.
  • The skipna parameter controls whether NA or null entries participate in the operation; it defaults to true.
  • The result is a Series of labels identifying maxima along the selected axis.

Common mistakes

  • An all-missing DataFrame, or any missing value when skipna=False, raises ValueError.
  • With numeric_only=True, non-numeric columns are excluded from the calculation.

Example: Find maximum locations

import pandas as pd

df = pd.DataFrame(
    {"temperature": [18, 23, 23], "rainfall": [4, 1, 7]},
    index=["Mon", "Tue", "Wed"],
)

print(df.idxmax())
print(df.idxmax(axis="columns"))

The first call finds the row label of each column's maximum; the second finds the column label of each row's maximum.

When to use it

Locate column maxima

For every metric column, identify the record label with the largest value.

Locate row maxima

For each record, determine which measured field has the greatest value.

Select numeric data

Restrict comparisons to numeric fields in mixed-type DataFrames.

Interview questions

QWhat does DataFrame.idxmax() return by default?

idxmax returns a Series containing the index label where the maximum occurs along the chosen axis. Its default axis is 0, so each result corresponds to a column.

Follow-up: How do you find maxima by row? Use df.idxmax(axis="columns"), which returns the relevant column label for every row.

QHow do you get the column containing each row's maximum?

Set axis="columns" (or axis=1) to evaluate across columns for each row. The result contains the column label associated with each row's maximum.

Follow-up: What happens if a row contains missing data? By default it is skipped; set skipna=False to make missing data an error.

QHow does skipna affect idxmax?

Missing values are excluded by default because skipna=True. If missing values are not skipped and any are present, or if the entire DataFrame is missing, the method raises ValueError.

Follow-up: How can you limit the operation to numeric columns? Pass numeric_only=True, which includes float, integer, and Boolean data.

  • DataFrame.max
  • Series.idxmax
  • numpy.ndarray.argmax

Source: pandas.DataFrame.idxmax — pandas 3.0.6 documentation. Checked 2026-10-15.