Skip to main content
PYSPARK • LESSON 232

Moving Average with Sliding Window Frames

How can we calculate a 7-day moving average of daily active users to smooth out weekend fluctuations?

Advanced3 Minutes810 XP
🤔 THE QUESTION

How can we calculate a 7-day moving average of daily active users to smooth out weekend fluctuations?

💡 WHAT IS IT?

rowsBetween(-6, Window.currentRow) defines a rolling sliding window of 7 discrete consecutive observations.

🎯 WHAT IS IT USED FOR?

Algorithmic trading signals, smoothing noisy sensor telemetry, and weekly trend analysis.

💻 EXAMPLE
w = Window.partitionBy("metric_name").orderBy("date").rowsBetween(-6, Window.currentRow)
df_moving_avg = df.withColumn("7_day_avg", avg("daily_value").over(w))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Moving Average with Sliding Window Frames.

  • Sliding window frame specification
  • rowsBetween(-6, Window.currentRow)
  • Moving average smoothing