Locate column maxima
For every metric column, identify the record label with the largest value.
Pandas · Module
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.
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.
DataFrame.idxmax(axis=0, skipna=True, numeric_only=False)| Name | Type | Description |
|---|---|---|
axis | {0 or 'index', 1 or 'columns'}, default 0 | Axis to evaluate: 0 or "index" evaluates down rows for each column; 1 or "columns" evaluates across columns for each row. Defaults to 0. |
skipna | bool, default True | Whether to omit NA/null values. Defaults to True. |
numeric_only | bool, default False | Whether to include only float, integer, or Boolean columns. Defaults to False. |
Series — indexes of maxima along the specified axis.
skipna is disabled.skipna parameter controls whether NA or null entries participate in the operation; it defaults to true.skipna=False, raises ValueError.numeric_only=True, non-numeric columns are excluded from the calculation.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.
For every metric column, identify the record label with the largest value.
For each record, determine which measured field has the greatest value.
Restrict comparisons to numeric fields in mixed-type DataFrames.
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.
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.
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.maxSeries.idxmaxnumpy.ndarray.argmax