Skip to main content
PYSPARK โ€ข LESSON 77

Unique Cardinality with countDistinct()

How do we count the exact number of distinct customer buyers who placed orders?

Intermediateโฑ 2 Minutesโญ 390 XP
๐Ÿค” THE QUESTION

How do we count the exact number of distinct customer buyers who placed orders?

๐Ÿ’ก WHAT IS IT?

countDistinct() calculates the exact number of unique values in a Column across all distributed partitions.

๐ŸŽฏ WHAT IS IT USED FOR?

Unique buyer metrics, daily active user counts, and cardinality analysis for indexing.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, countDistinct

df.select(countDistinct(col("customer_id")).alias("unique_buyers")).show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Unique Cardinality with countDistinct().

  • โœ“ Import countDistinct function
  • โœ“ Calculate unique customer cardinality
  • โœ“ Display unique buyer metric