Signup/Sign In

Pandas Series argmin() Method

In this tutorial, we will learn the python pandas Series.argmin() method. This method returns the integer value which indicates the position where the smallest value exists.

The below shows the syntax of the Series.argmin() method.

Syntax

Series.argmin(axis=None, skipna=True, *args, **kwargs)

Parameters

axis: It is None. It is the dummy argument for consistency with Series.

skipna: It is the bool(True or False), and the default value is True. It excludes all null values when showing the result.

*args, **kwargs: It is the additional arguments and keywords for compatibility with NumPy.

Example: Get the position of smaller value using the Series.argmin() Method

Let's apply the Series.argmin() method to the two Series and get the position of the smallest value. See the below example.

Here, in Series s_1, there are different values and in s_2, it consists of repeated values. The Series.argmin() method returns a position and if the smallest value present in a different location, it returns the position of the first row.

#importing pandas as pd
import pandas as pd
#creating Series
s_1= pd.Series([45,10,78,22])
s_2= pd.Series([45,14,11,11])
print("In Series s_1 the minimum value is in the position:",s_1.argmin())
print("In Series s_2 the minimum value is in the position:",s_2.argmin())


In Series s_1 the minimum value is in the position: 1
In Series s_2 the minimum value is in the position: 2

Example: Get the position of the smallest value of the Series consisting of null values

Here, in this example, the Series elements consist of null values, and by default the Series.argmin() method ignores the null values and returns the position of the smallest value just by considering the integer elements. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating Series
s = pd.Series([np.nan,5,9,np.nan])
print("The minimum value is in the position:",s.argmin())


The minimum value is in the position: 1

Example: Get the position of the smallest value of the Series consisting of null values

If we want to consider null values when getting the minimum value, we can achieve it by passing the skipna=False in the Series.argmin() method. See the below example. It returns the position as -1 for null values.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating Series
s = pd.Series([12,90,100,np.nan,np.nan,120])
print("The minimum value is in the position:",s.argmin(skipna=False))


The minimum value is in the position: -1

Conclusion

In this tutorial, we understand the Series.argmin() method of the data frame. We learned the syntax and parameters of Series.argmin() method and created different examples to better understand this topic.



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.