Skip to main content
PYSPARK โ€ข LESSON 44

Conditional Logic with when()

How do we evaluate conditional business rules to classify high-scoring customers as Platinum?

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

How do we evaluate conditional business rules to classify high-scoring customers as Platinum?

๐Ÿ’ก WHAT IS IT?

when(condition, value) evaluates a boolean Column condition and assigns a value when True.

๐ŸŽฏ WHAT IS IT USED FOR?

Customer loyalty tiering, alert status labeling, and rule-based business categorization.

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

df = df.withColumn(
    "tier",
    when(col("score") >= 90, "Platinum")
)

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Conditional Logic with when().

  • โœ“ Import when function
  • โœ“ Apply conditional threshold check
  • โœ“ Assign Platinum loyalty tier