Pandas Data Series: Find the index of the first occurrence of the smallest and largest value of a given series
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'.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics