PySpark Koans

Learn by fixing tests

Progress0/39

© 2025-2026 Alex Cole. All Rights Reserved.

Spark Koans is an independent community tool.

Window FunctionsKoan 25

Window Functions - Lag and Lead

Access previous or next row values. Replace ___ with the correct code.

How it works: Replace the ___ blanks in the code editor with the correct PySpark code, then hit Run Code. Stuck? Try the Hint button.
Setup (read-only)
from pyspark.sql.window import Window
from pyspark.sql.functions import lag, lead, col

data = [
    ("2024-01-01", 100),
    ("2024-01-02", 150),
    ("2024-01-03", 120),
    ("2024-01-04", 200)
]
df = spark.createDataFrame(data, ["date", "price"])
Your CodeCtrl/Cmd+Enter to run
Output
Output will appear here...