How do we look back to fetch the previous order amount for each customer to compute spending changes?
lag(col, offset).over(windowSpec) retrieves the value of a column from offset rows prior to the current row.
Calculating period-over-period growth, session duration, and customer spending deltas.
from pyspark.sql.window import Window
from pyspark.sql.functions import col, lag
window_spec = Window.partitionBy("customer_id").orderBy("order_date")
df = df.withColumn("prev_order_amount", lag(col("amount"), 1).over(window_spec))Practice typing production-grade PySpark code for Fetching Prior Row Values with lag().