Skip to main content
PYSPARK • LESSON 51

Trimming Whitespace with trim()

How do we strip leading and trailing whitespace from customer name fields during ingestion?

Intermediate2 Minutes300 XP
🤔 THE QUESTION

How do we strip leading and trailing whitespace from customer name fields during ingestion?

💡 WHAT IS IT?

trim() removes all leading and trailing whitespace spaces from a string Column.

🎯 WHAT IS IT USED FOR?

Cleansing raw user input from web forms, legacy CSV files, and mainframe fixed-width dumps.

💻 EXAMPLE
from pyspark.sql.functions import col, trim

df = df.withColumn("customer_name", trim(col("raw_name")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Trimming Whitespace with trim().

  • Import trim string function
  • Remove leading and trailing spaces
  • Cleanse raw customer names