﻿ Pandas Data Series: Find the positions of the values neighboured by smaller values on both sides in a given series - w3resource

# 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.

Pictorial Presentation:

Sample Solution:

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)[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]
```

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

﻿

## Python: Tips of the Day

Returns a list with n elements removed from the beginning

Example:

```def tips_take(itr, n = 1):
return itr[:n]
print(tips_take([1, 2, 3], 5))
print(tips_take([1, 2, 3], 0))
```

Output:

```[1, 2, 3]
[]
```