多欄位的滾動彙總
如果你同時需要對多個欄位做滾動統計怎麼辦?.rolling() 方法可以在時間型視窗上,對多個欄位同時計算彙總。
polars 已載入為 pl。bikes DataFrame 可用,包含 time、rentals 和 temp 欄位。
本練習屬於課程
使用 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")
)