Skip to main content
PYSPARK โ€ข LESSON 110

Dense Ranking without Gaps with dense_rank()

How do we compute continuous rankings without gaps for tied revenue values (e.g. 1, 2, 2, 3)?

Advancedโฑ 2 Minutesโญ 610 XP
๐Ÿค” THE QUESTION

How do we compute continuous rankings without gaps for tied revenue values (e.g. 1, 2, 2, 3)?

๐Ÿ’ก WHAT IS IT?

dense_rank().over(windowSpec) computes ranks without gap skips following tied values.

๐ŸŽฏ WHAT IS IT USED FOR?

Pricing tier classifications, top-N medal distributions, and executive revenue ranking.

๐Ÿ’ป EXAMPLE
from pyspark.sql.window import Window
from pyspark.sql.functions import col, dense_rank

window_spec = Window.partitionBy("region").orderBy(col("revenue").desc())
df = df.withColumn("dense_rank", dense_rank().over(window_spec))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Dense Ranking without Gaps with dense_rank().

  • โœ“ Import dense_rank function
  • โœ“ Calculate dense rank without gaps
  • โœ“ Rank regional revenues seamlessly