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?

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