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?

Advancedโฑ 2 Minutesโญ 550 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