Signup/Sign In

Pandas DataFrame idxmin() Method

In this tutorial, we will discuss and learn the Python pandas DataFrame.idxmin() method. This method is used to get the index of the first occurrence of minimum 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 shows the syntax of the DataFrame.idxmin() method.

Syntax

DataFrame.idxmin(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 1: Find index using the DataFrame.idxmin() Method

Here, we are using a dataset and getting the index for the minimum value in each column using the DataFrame.idxmin() 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 minimum value over the row axis----")
print(df.idxmin())


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

Example 2: Find index using the DataFrame.idxmin() Method

Here, we are getting the index for the minimum value in each row using the DataFrame.idxmin() 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 minimum value over the column axis----")
print(df.idxmin(axis="columns"))


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

Example 2: Find index using the DataFrame.idxmin() Method

We have a dataset consisting of null values and trying to get the index for the minimum value in each row using the DataFrame.idxmin() method. See the below example.

#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 minimum value over the column axis----")
print(df.idxmin(axis="columns"))


-----The DataFrame is----
Marks_1 Marks_2
Kannada 85.0 NaN
English NaN 46.0
Science 45.0 NaN
----- Index of the minimum 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.idxmin() method. We learned the syntax and by applying this method on the DataFrame we understand the working of this method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.