Skip to main content
PYSPARK • LESSON 105

Window Specification Foundations

How do we define a Window specification partitioned by department and ordered by descending salary?

Advanced2 Minutes550 XP
🤔 THE QUESTION

How do we define a Window specification partitioned by department and ordered by descending salary?

💡 WHAT IS IT?

Window.partitionBy().orderBy() creates a WindowSpec defining the grouping and row ordering for analytical calculations.

🎯 WHAT IS IT USED FOR?

Ranking, cumulative sums, moving averages, and lead/lag calculations without collapsing rows.

💻 EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import col

windowSpec = Window.partitionBy("department_id").orderBy(col("salary").desc())

🎯 Mission Objectives

Practice typing production-grade PySpark code for Window Specification Foundations.

  • Import Window class
  • Define partition and order specification
  • Prepare WindowSpec for analytical functions