w3resource

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

 

Pandas: Data Series Exercise-39 with Solution

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

Python 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 Python skills with w3resource's quiz



Python: Tips of the Day

Merging two dicts in Python 3.5+ with a single expression

Example:

# How to merge two dictionaries
# in Python 3.5+

x = {'p': 1, 'q': 3}
y = {'q': 5, 'r': 8}

z = {**x, **y}

z
{'r': 4, 'p': 1, 'q': 3}

z = dict(x, **y)
print(z)

Output:

{'p': 1, 'q': 5, 'r': 8}