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?

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