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?

Intermediate2 Minutes320 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