How to Get the Position of Minimum Value of a pandas Series?


To get the position of minimum value of a pandas series object we can use a function called argmin().

The argmin() is the method of the pandas series constructor, which is used to get the row position of the smallest value from the series. The output of the argmin() method is an integer value. If the pandas series object having the Nan values, then the argmin() method will identify the smallest number by neglecting those Nan values.

If the minimum value is located in multiple index positions, then the first occurrence value position is taken as output.

Example 1

# import pandas package
import pandas as pd
import numpy as np

# create a pandas series
s = pd.Series(np.random.randint(1,100, 10))
print(s)

# Apply argmin function
print('Output of argmin:', s.argmin())

Explanation

Let’s create a pandas series object with 10 random integer values between 1 to 100 range and then apply the argmin function to get the position of minimum value of the series object.

Initially, we have imported the NumPy package for creating a range of random integer values.

Output

0 76
1 58
2 42
3 81
4  7
5 85
6 75
7 25
8 61
9 57
dtype: int32

Output of argmin: 4

The argmin() method of pandas series object is returned an integer value “4” for the following example, and that integer value represents the position of the minimum value of the given series object.

Example 2

import pandas as pd

# creating range sequence of dates
dates = pd.date_range('2021-06-01', periods=6, freq='D')

#creating pandas Series with date index
s = pd.Series([43,21,40,54,10,23], index= dates)
print (s)

# Apply argmin function
print('Output of argmin:', s.argmin())

Explanation

Let’s create another pandas series object using a list of integers with date_range index labels. After that, apply the argmin() method over the data of the series object “s” to get the position of the minimum value of the given series object.

Output

2021-06-01 43
2021-06-02 21
2021-06-03 40
2021-06-04 54
2021-06-05 10
2021-06-06 23
Freq: D, dtype: int64

Output of argmin: 4

The output of the argmin() method for this example is integer value “4”, which means the value at index label “2021-06-05” is having a minimum value over the data present in the given series object.

Updated on: 09-Mar-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements