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?

Intermediateโฑ 2 Minutesโญ 250 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