41v4
41v4

Reputation: 21

How to find min value between several max values in a single column?

Edit: please look at the end of this question. Made an edit.

I need to find minimum values between every two maximum values in a single column. Minimum of the maximum values should be more than 10.

Here is the sample:

Price Vol.
95    7
90    13
85    19
80    16
75    12
70    5
65    8
60    15
55    22
50    35
45    20
40    8
35    3
30    6
25    11
20    20
15    25
10    16
5     8

And I would like to know how I would be able to get like this:

Price Vol. Result
85    19    max
70    5     min
50    35    max
35    3     min
15    25    max

More explained here

img

Edit1: After Quang Hoang correct answer, I have noticed that my sample looks too good (I mean not realistic).

Here is the new more realistic sample:

Price Vol.
30    7
29    13
28    19
27    18
26    21
25    5
24    8
23    15
22    22
21    29
20    20
21    26
20    28
19    25
18    11
17    15
16    11
15    7
14    3
13    12
12    18
11    33
10    25

And I would like to know how I would be able to get like this:

Price Vol. Result
26    21   max
25    5    min
21    29   max
14    3    min
11    33   max

As you can see I need to identify ranges of values greater than 10, then find a max in that range and finally find a min value between those identified maximum values.

Upvotes: 0

Views: 291

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

For your data, you can mask max and min by comparing to the neighbors:

diff = df['Vol.'].diff()
is_max = diff.gt(0) & diff.shift(-1).lt(0)
is_min = diff.shift().lt(0) & diff.gt(0)

df['Result'] = np.select([is_max, is_min], ['max', 'min'])

df[df['Result'].ne('0')]

Output:

    Price  Vol. Result
2      85    19    max
6      65     8    min
9      50    35    max
13     30     6    min
16     15    25    max

Upvotes: 1

Related Questions