w3resource

Pandas Data Series: Find the index of the first occurrence of the smallest and largest value of a given series

 


39. Index of Extremes

Write a Pandas program to find the index of the first occurrence of the smallest and largest value of a given series.

Sample Solution :

Python Code :

import pandas as pd
nums = pd.Series([1, 3, 7, 12, 88, 23, 3, 1, 9, 0])
print("Original Series:")
print(nums)
print("Index of the first occurrence of the smallest and largest value of the said series:")
print(nums.idxmin())
print(nums.idxmax())

Sample Output:

Original Series:
0     1
1     3
2     7
3    12
4    88
5    23
6     3
7     1
8     9
9     0
dtype: int64
Index of the first occurrence of the smallest and largest value of the said series:
9
4

Explanation:

nums = pd.Series([1, 3, 7, 12, 88, 23, 3, 1, 9, 0]): This code creates a Pandas Series object 'nums' containing ten integers.

nums.idxmin(): This code returns the index label of the minimum value in the ‘nums’ Pandas series.

nums.idxmax(): This code returns the index of the maximum value in the Pandas Series object 'nums'.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to find the index of the first occurrence of the minimum and maximum values in a Series with duplicate extremes.
  • Write a Pandas program to locate the first occurrence indices of the smallest and largest values in a Series after applying a transformation.
  • Write a Pandas program to determine the indices of min and max values in a Series and then swap their positions.
  • Write a Pandas program to find the index of the first occurrence of the minimum and maximum values and then extract corresponding elements.

Go to:


Previous: Write a Pandas program to check the equality of two given series.
Next: Write a Pandas program to check inequality over the index axis of a given dataframe and a given series.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.