Pandas滚动函数与Max

zf2sa74q  于 2023-09-29  发布在  其他
关注(0)|答案(1)|浏览(130)

我有以下代码:

  1. import pandas as pd
  2. # Sample DataFrame
  3. df = pd.DataFrame({'clicks': [i for i in range(1000)]})
  4. # Calculate the max of the next 140 rows for each row
  5. roll = df['clicks'].rolling(window=141, min_periods=1).max()
  6. print(roll)

但它没有返回预期的输出:

  1. 0 141.0
  2. 1 142.0
  3. 2 143.0
  4. 3 144.0
  5. 4 145.0

但它输出:

  1. 0 0.0
  2. 1 1.0
  3. 2 2.0
  4. 3 3.0
  5. 4 4.0

我做错了什么?

qrjkbowd

qrjkbowd1#

您需要将其向前移动140行,以便窗口 * 包括接下来的140行 *,而不仅仅是第一行,第二行,第三行等,直到999行。

  1. # Calculate the rolling max of the next 140 rows for each row
  2. roll = df['clicks'].shift(-140).rolling(window=141, min_periods=1).max()

相关问题