Skip to main content
PYSPARK • LESSON 69

First Non-Null Selection with coalesce()

How do we select the first available phone number from mobile, home phone, or a fallback 'N/A' literal?

Intermediate2 Minutes360 XP
🤔 THE QUESTION

How do we select the first available phone number from mobile, home phone, or a fallback 'N/A' literal?

💡 WHAT IS IT?

coalesce() evaluates Column arguments from left to right and returns the first non-null value.

🎯 WHAT IS IT USED FOR?

Customer contact resolution, priority channel fallback, and merging multi-source address fields.

💻 EXAMPLE
from pyspark.sql.functions import col, coalesce, lit

df = df.withColumn(
    "contact_phone",
    coalesce(col("mobile"), col("home_phone"), lit("N/A"))
)

🎯 Mission Objectives

Practice typing production-grade PySpark code for First Non-Null Selection with coalesce().

  • Import coalesce and lit
  • Evaluate contact phone priority order
  • Assign resolved phone with fallback