Pandas Data Series: Find the positions of the values neighboured by smaller values on both sides in a given series
Pandas: Data Series Exercise-32 with Solution
Write a Pandas program to find the positions of the values neighboured by smaller values on both sides in a given series.
Python Code :
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) + 1 print(result)
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]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to compute the Euclidean distance between two given series.
Next: Write a Pandas program to replace missing white spaces in a given string with the least frequent character.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Returns a list with n elements removed from the beginning
def tips_take(itr, n = 1): return itr[:n] print(tips_take([1, 2, 3], 5)) print(tips_take([1, 2, 3], 0))
[1, 2, 3] 
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework