Pandas DataFrame idxmax() Method

In this tutorial, we will discuss and learn the Python pandas DataFrame.idxmax() method. This method is used to get the index of the first occurrence of maximum over the requested axis. If any null values or missing values are there, that will be excluded. This method returns a Series that consists of indexes of maxima along the specified axis. If the row or column in the DataFrame is empty, it raises a ValueError.

The below is the syntax of the DataFrame.idxmax() method.

Syntax

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

Parameters

axis: '0' or 'index' represents the row-wise and '1' or 'column' represents the column-wise. It represents which axis is to use.

skipna: It represents the bool(True or False), and the default is True. When this parameter is True, it excludes all null values or missing values. If an entire row or column in the DataFrame is null values, the result will also be NA.

Example: Getting the index using the DataFrame.idxmax() Method

Consider a dataset and get the index for the maximum value in each column using the DataFrame.idxmax() method. See the below example.

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,90,45],'Marks_2': [85,96,100]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the row axis----")
print(df.idxmax())

Once we run the code we will get the following output.


-----The DataFrame is----
Marks_1 Marks_2
Kannada 85 85
English 90 96
Science 45 100
----- Index of the maximum value over the row axis----
Marks_1 English
Marks_2 Science
dtype: object

Example: Getting the index using the DataFrame.idxmax() Method

Consider a dataset and get the index for the maximum value in each row using the DataFrame.idxmax() method. See the below example.

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,90,45],'Marks_2': [95,46,100]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the column axis----")
print(df.idxmax(axis="columns"))

Once we run the code we will get the following output.


-----The DataFrame is----
Marks_1 Marks_2
Kannada 85 95
English 90 46
Science 45 100
----- Index of the maximum value over the column axis----
Kannada Marks_2
English Marks_1
Science Marks_2
dtype: object

Example: Getting The index using the DataFrame.idxmax() Method

This dataset consisting of null values and we are trying to get the index for the maximum value in each row using the DataFrame.idxmax() method.

#importing pandas as pd
import pandas as pd
df = pd.DataFrame({'Marks_1': [85,None,45],'Marks_2': [None,46,None]},index=['Kannada', 'English', 'Science'])
print("-----The DataFrame is----")
print(df)
print("----- Index of the maximum value over the column axis----")
print(df.idxmax(axis="columns"))

Once we run the code we will get the following output.


-----The DataFrame is----
Marks_1 Marks_2
Kannada 85.0 NaN
English NaN 46.0
Science 45.0 NaN
----- Index of the maximum value over the column axis----
Kannada Marks_1
English Marks_2
Science Marks_1
dtype: object

Conclusion:

In this tutorial, we learned the Python pandas DataFrame.idxmax() method. We learned the syntax of this method and applied to the DataFrame to understand the DataFrame.idxmax() method.