Skip to main content
PYSPARK • LESSON 148

Applying UDFs to DataFrame Columns

How do we invoke a registered UDF inside withColumn() to create a masked email column?

Expert2 Minutes780 XP
🤔 THE QUESTION

How do we invoke a registered UDF inside withColumn() to create a masked email column?

💡 WHAT IS IT?

Calling the UDF object with Column arguments inside withColumn() to evaluate row-by-row Python logic.

🎯 WHAT IS IT USED FOR?

Anonymizing PII fields in staging tables before publishing to shared analytical workspaces.

💻 EXAMPLE
from pyspark.sql.functions import col

df = df.withColumn("masked_email", mask_email_udf(col("email")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Applying UDFs to DataFrame Columns.

  • Invoke UDF with col() argument
  • Apply custom masking logic
  • Store result in new masked_email column