開始使用免費開始

多欄位的滾動彙總

如果你同時需要對多個欄位做滾動統計怎麼辦?.rolling() 方法可以在時間型視窗上,對多個欄位同時計算彙總。

polars 已載入為 plbikes DataFrame 可用,包含 timerentalstemp 欄位。

本練習屬於課程

使用 Polars 進行資料轉換

檢視課程

練習說明

  • time 欄位上設定 3 小時的滾動視窗。
  • 在彙總中計算 rentals 的總和,以及 temp 的平均。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Set up the 3-hour rolling window
bikes.rolling(index_column="____", period="____").agg(
  
    # Calculate sum of rentals and mean of temp
    pl.col("rentals").____().alias("rolling_total"),
    pl.col("temp").____().alias("rolling_mean_temp")
)
編輯並執行程式碼