Skip to main content
PYSPARK • LESSON 42

Injecting Constant Literals with lit()

How do we append a constant environment tag to every record using the lit() function?

Intermediate2 Minutes260 XP
🤔 THE QUESTION

How do we append a constant environment tag to every record using the lit() function?

💡 WHAT IS IT?

lit() converts a primitive Python string, integer, or float into a Spark Column literal.

🎯 WHAT IS IT USED FOR?

Tagging pipeline source systems, environment identifiers, partition keys, and audit metadata.

💻 EXAMPLE
from pyspark.sql.functions import lit

df = df.withColumn("environment", lit("PRODUCTION"))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Injecting Constant Literals with lit().

  • Import lit function
  • Create Column literal value
  • Append constant environment tag