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)?

Advanced2 Minutes610 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