Skip to main content
PYSPARK • LESSON 84

Category Benchmarks with avg()

How do we compute the average product review rating for each merchandise category?

Intermediate2 Minutes410 XP
🤔 THE QUESTION

How do we compute the average product review rating for each merchandise category?

💡 WHAT IS IT?

Grouping by product category and applying the avg() aggregate function to rating scores.

🎯 WHAT IS IT USED FOR?

Product catalog benchmarking, merchant quality monitoring, and recommendation engine features.

💻 EXAMPLE
from pyspark.sql.functions import avg, col

df.groupBy("category").agg(avg(col("rating")).alias("avg_rating")).show()

🎯 Mission Objectives

Practice typing production-grade PySpark code for Category Benchmarks with avg().

  • Group by product category
  • Calculate mean review rating
  • Alias average rating metric