How does the idxmax() method work?

QuestionsQuestions8 SkillsProPandas Textual DataAug, 06 2025
0249

The idxmax() method in pandas is used to return the index of the first occurrence of the maximum value along a specified axis in a DataFrame. Here's how it works:

Syntax

DataFrame.idxmax(axis=0, skipna=True, *args, **kwargs)

Parameters

  • axis: {0 or 'index', 1 or 'columns'}, default 0

    • 0 or 'index': Return the index of the maximum value for each column.
    • 1 or 'columns': Return the index of the maximum value for each row.
  • skipna: bool, default True

    • If True, it will ignore NaN values when calculating the maximum.

Returns

  • The method returns a Series containing the index of the maximum value for each column or row, depending on the specified axis.

Example

Here's an example to illustrate how idxmax() works:

import pandas as pd

# Sample DataFrame
data = {
    'A': [1, 3, 2],
    'B': [4, 2, 5],
    'C': [7, 8, 6]
}
df = pd.DataFrame(data)

# Get the index of the maximum value for each column
max_index_columns = df.idxmax()
print("Index of max values for each column:")
print(max_index_columns)

# Get the index of the maximum value for each row
max_index_rows = df.idxmax(axis=1)
print("\nIndex of max values for each row:")
print(max_index_rows)

Output

Index of max values for each column:
A    1
B    2
C    1
dtype: int64

Index of max values for each row:
0    C
1    C
2    B
dtype: object

In this example, idxmax() returns the index of the maximum values for each column and each row, allowing you to easily identify where the maxima occur in your DataFrame.

0 Comments

no data
Be the first to share your comment!