import pandas as pd
df = pd.DataFrame({
'Sales': [250, 450, 300],
'Profit': [40, 90, 60]
}, index=['Store_A', 'Store_B', 'Store_C'])
print(df.idxmax())
Expected Output:
Sales Store_B
Profit Store_B
dtype: object
The DataFrame.idxmax() method returns the index label where the maximum value occurs along a specified axis. While traditional aggregation functions like max() return the actual peak numeric value (e.g., peak temperature or highest revenue), idxmax() pinpoints the exact row or column label corresponding to that maximum entry. In analytical workloads—such as identifying top-performing branch locations, finding peak trading hours in financial datasets, or determining winner categories per row—idxmax() serves as an essential index lookup tool.
DataFrame.idxmax(axis=0, skipna=True, numeric_only=False)axis: 0 or 'index' (find row label of peak value within each column; default), 1 or 'columns' (find column label of peak value across each row).skipna: Boolean (default True). Excludes missing/NaN values. If False and a column contains NaN, the method returns NaN.numeric_only: Boolean (default False). Includes only float, int, and boolean columns.axis=1.Setting axis=1 evaluates each row individually and returns the column header containing the highest numerical value.
import pandas as pd
df_scores = pd.DataFrame({
'Math': [88, 92, 79],
'Science': [95, 85, 91],
'English': [70, 89, 95]
}, index=['Student_1', 'Student_2', 'Student_3'])
# Find top subject per student
df_scores['Top_Subject'] = df_scores.idxmax(axis=1)
print(df_scores)
Expected Output:
Math Science English Top_Subject
Student_1 88 95 70 Science
Student_2 92 85 89 Math
Student_3 79 91 95 English
Combine groupby() with idxmax() to pull entire row records corresponding to category peak values.
import pandas as pd
df_sales = pd.DataFrame({
'Region': ['East', 'East', 'West', 'West'],
'Rep': ['Alice', 'Bob', 'Charlie', 'David'],
'Revenue': [5000, 7500, 6200, 4800]
})
# Get index labels of max revenue per region
idx_max = df_sales.groupby('Region')['Revenue'].idxmax()
print(df_sales.loc[idx_max])
Expected Output:
Region Rep Revenue
1 East Bob 7500
2 West Charlie 6200
idxmax() on an empty Series or DataFrame filtered down to zero rows.idxmax().idxmax() returns only the first index label encountered.df[df['col'] == df['col'].max()].
Q: What is the difference between idxmax() and max()?
max() returns the maximum numerical quantity (e.g., 7500). idxmax() returns the row index label where that maximum quantity lives (e.g., index 1 or 'Store_B').
Q: How does idxmax() handle NaN values?
By default, skipna=True ignores missing entries. If all entries in a column are NaN, idxmax() returns NaN.
Explore interactive code cells, test GroupBy peak lookups, run row-wise index extractions, and execute self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.