Q:

Write a Pandas program to find the positions of the values neighboured by smaller values on both sides in a given series

0

Write a Pandas program to find the positions of the values neighboured by smaller values on both sides in a given series

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import pandas as pd
import numpy as np
nums = pd.Series([1, 8, 7, 5, 6, 5, 3, 4, 7, 1])
print("Original series:")
print(nums)
print("\nPositions of the values surrounded by smaller values on both sides:")
temp = np.diff(np.sign(np.diff(nums)))
result = np.where(temp == -2)[0] + 1
print(result)

Sample Output:

Original series:
0    1
1    8
2    7
3    5
4    6
5    5
6    3
7    4
8    7
9    1
dtype: int64

Positions of the values surrounded by smaller values on both sides:
[1 4 8]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now