Skip to main content
PYSPARK • LESSON 41

Adding Columns with withColumn()

How do we compute and append an annual bonus column to an employee compensation DataFrame?

Intermediate2 Minutes250 XP
🤔 THE QUESTION

How do we compute and append an annual bonus column to an employee compensation DataFrame?

💡 WHAT IS IT?

withColumn(colName, colExpr) returns a new DataFrame with an added or replaced column computed from an expression.

🎯 WHAT IS IT USED FOR?

Feature engineering, financial calculations, data enrichment, and derived metric computation.

💻 EXAMPLE
from pyspark.sql.functions import col

df = df.withColumn("annual_bonus", col("salary") * 0.15)

🎯 Mission Objectives

Practice typing production-grade PySpark code for Adding Columns with withColumn().

  • Use withColumn() to add new field
  • Apply arithmetic multiplication on salary
  • Append computed bonus column