[英]Find highest value among the next K-consecutive rows in Pandas?
I'm working with a time-series price data and I want to know how high price can reach in the next K-rows for every row. 我正在使用时间序列价格数据,我想知道在接下来的K行中每行可以达到多高的价格。
I can think of achieving it somehow using .argmax()
while filtering dataframe based on time, however there must be a simpler built-in solution. 我可以考虑在基于时间过滤数据帧时使用
.argmax()
以某种方式实现它,但是必须有一个更简单的内置解决方案。
For example: 例如:
Price
1 $10
2 $11
3 $15
4 $18
5 $13
6 $4
7 $25
For K=2, here's what I want: 对于K = 2,这是我想要的:
Price Highest_In_Next_2_Rows
1 $10 $15
2 $11 $18
3 $15 $18
4 $18 $13
5 $13 $25
6 $4 $25
7 $25 NaN
You can achieve this using pandas rolling and shift function. 您可以使用熊猫滚动和移位功能来实现此目的。
Essentially you find the rolling max over the previous k observations and then you shift the series by k, so that the max for t is the one calculated over (t+1, ..., t+k). 本质上,您可以找到前k个观测值的滚动最大值,然后将序列移位k,以便t的最大值是在(t + 1,...,t + k)上计算得出的值。
import pandas as pd
import numpy as np
ts = pd.Series([10, 11, 15, 18, 13, 4, 25])
k = 2
res = ts.rolling(k).max().shift(-k)
pd.concat([ts, res], axis = 1)
output: 输出:
# 0 1
# 0 10 15.0
# 1 11 18.0
# 2 15 18.0
# 3 18 13.0
# 4 13 25.0
# 5 4 NaN
# 6 25 NaN
The problem of this solution is that it doesn't give results for the last k observations. 该解决方案的问题在于,它没有给出最后k个观测值的结果。
A workaround is the following: You consider the series in reverse order and calculate the rolling max over the past k observations (giving results when there is at least one observation). 一种变通方法是:您以相反的顺序考虑序列,并计算过去k个观测值的滚动最大值(当存在至少一个观测值时给出结果)。 Then you lag by one day as you don't want today's price to be included and you reverse again to go back to the original order.
然后,您将延迟一天,因为您不希望将今天的价格包括在内,然后再次反转以返回原始订单。
res = ts[::-1].rolling(k,1).max().shift(1)[::-1]
Which replicates exactly the desired output: 完全复制所需的输出:
# 0 1
#0 10 15.0
#1 11 18.0
#2 15 18.0
#3 18 13.0
#4 13 25.0
#5 4 25.0
#6 25 NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.