【问题标题】:Pandas: How to find the low after a high within a rolling windowPandas:如何在滚动窗口中找到高点后的低点
【发布时间】:2021-04-18 01:24:11
【问题描述】:

对于一系列数字,我试图在滚动窗口内找到高点之后的低点。我能够计算窗口内的高点,但不能计算同一窗口内的低点。我正在使用 Pandas 并试图获取最高索引并将其用作某种类型的参考,但我无法让它工作。

这里是一些设置问题的代码:

 dates = pd.date_range("20130101", periods=15)
 temps = {'Temperature' :[16, 3, 26, 56, 2, 92, 54, 98, 73, 68, 80, 18, 75, 24, 12]}
 df = pd.DataFrame(temps, index=dates, columns = ['Temperature'])
 df['RollMax'] = df['Temperature'].rolling(5).max()
 # df['Low_After_High'] = ### Lowest value after high has been reached within the window

下面是输出的样子:

【问题讨论】:

    标签: python pandas max min rolling-computation


    【解决方案1】:

    让我们做applyidxmax

    df['Low_After_High'] = df.Temperature.rolling(5).apply(lambda x : min(x[pd.Series(x).idxmax():]))
    2013-01-01     NaN
    2013-01-02     NaN
    2013-01-03     NaN
    2013-01-04     NaN
    2013-01-05     2.0
    2013-01-06    92.0
    2013-01-07    54.0
    2013-01-08    98.0
    2013-01-09    73.0
    2013-01-10    68.0
    2013-01-11    68.0
    2013-01-12    18.0
    2013-01-13    18.0
    2013-01-14    18.0
    2013-01-15    12.0
    Freq: D, Name: Temperature, dtype: float64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-21
      • 2012-09-07
      • 2021-04-21
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多