Skip to main content
PYSPARK โ€ข LESSON 47

Overwriting Existing Columns with Transformations

How do we round salary numbers in-place to 2 decimal places without creating new column names?

Intermediateโฑ 2 Minutesโญ 320 XP
๐Ÿค” THE QUESTION

How do we round salary numbers in-place to 2 decimal places without creating new column names?

๐Ÿ’ก WHAT IS IT?

Passing an existing column name to withColumn() replaces the original column with the transformed expression.

๐ŸŽฏ WHAT IS IT USED FOR?

In-place data sanitization, rounding currency values, and replacing raw text with clean tokens.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, round

df = df.withColumn("salary", round(col("salary"), 2))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Overwriting Existing Columns with Transformations.

  • โœ“ Replace existing column in-place
  • โœ“ Apply round() transformation
  • โœ“ Preserve clean schema names