[英]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. 最大值的最小值应大于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 这里有更多解释
Edit1: After Quang Hoang correct answer, I have noticed that my sample looks too good (I mean not realistic). Edit1:在Quang Hoang给出正确答案之后,我注意到我的样本看起来太好了(我的意思是不现实)。
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. 如您所见,我需要确定大于10的值的范围,然后找到该范围内的最大值,最后找到所标识的最大值之间的最小值。
For your data, you can mask max
and min
by comparing to the neighbors: 对于您的数据,可以通过与邻居进行比较来屏蔽max
和min
:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.