Skip to main content
PYSPARK โ€ข LESSON 119

Top-N Records per Category with Window Filtering

How do we filter for only the top 3 best-selling products in each merchandise category?

Advancedโฑ 3 Minutesโญ 720 XP
๐Ÿค” THE QUESTION

How do we filter for only the top 3 best-selling products in each merchandise category?

๐Ÿ’ก WHAT IS IT?

Ranking items with row_number() over category partitions, filtering for rank <= 3, and dropping rank.

๐ŸŽฏ WHAT IS IT USED FOR?

Leaderboard generation, featured product recommendation carousels, and category digest reports.

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

window_spec = Window.partitionBy("category").orderBy(col("sales").desc())
top3_df = df.withColumn("rank", row_number().over(window_spec)) \
            .filter(col("rank") <= 3) \
            .drop("rank")

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Top-N Records per Category with Window Filtering.

  • โœ“ Rank products by sales per category
  • โœ“ Filter top 3 items with boolean predicate
  • โœ“ Cleanly drop temporary rank column