Pandas DataFrame.idxmax() Function

Minahil Noor Jan 30, 2023
  1. Syntax of pandas.DataFrame.idxmax():
  2. Example Codes: DataFrame.idxmax() Method to Find Indexes of Maximum Values Row-Wise
  3. Example Codes: DataFrame.idxmax() Method to Find Indexes of Maximum Values Column-Wise
Pandas DataFrame.idxmax() Function

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in rows or columns.

Syntax of pandas.DataFrame.idxmax():

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

Parameters

axis It is an integer or string type parameter. It specifies the axis to use. 0 or index for rows, 1 or columns for columns.
skipna It is a Boolean parameter. This parameter specifies excluding null values. If an entire row or column is null, the result will be NA.

Return

It returns a Series that contains the indexes of maximum values along the specified axis.

Example Codes: DataFrame.idxmax() Method to Find Indexes of Maximum Values Row-Wise

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

series = dataframe.idxmax()
print("The Indexes are: \n")
print(series)

Output:

The Original Data frame is: 

   Attendance  Obtained Marks
0          60              90
1         100              75
2          80              82
3          78              64
4          95              45
The Indexes are: 

Attendance        1
Obtained Marks    0
dtype: int64

The function has returned the indexes of maximum Attendance and Obtained Marks

Example Codes: DataFrame.idxmax() Method to Find Indexes of Maximum Values Column-Wise

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

series = dataframe.idxmax(axis= 1)
print("The Indexes are: \n")
print(series)

Output:

The Original Data frame is: 

   Attendance  Obtained Marks
0          60              90
1         100              75
2          80              82
3          78              64
4          95              45
The Indexes are: 

0    Obtained Marks
1        Attendance
2    Obtained Marks
3        Attendance
4        Attendance
dtype: object

The function has returned the indexes column-wise.

Related Article - Pandas DataFrame