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?

Intermediate2 Minutes390 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