Skip to main content
PYSPARK โ€ข LESSON 53

Right Trimming with rtrim()

How do we remove trailing spaces from product description fields?

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

How do we remove trailing spaces from product description fields?

๐Ÿ’ก WHAT IS IT?

rtrim() strips whitespace from only the right (trailing) side of string Column values.

๐ŸŽฏ WHAT IS IT USED FOR?

Cleaning database CHAR columns that automatically pad trailing whitespace.

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

df = df.withColumn("trimmed_desc", rtrim(col("description")))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Right Trimming with rtrim().

  • โœ“ Import rtrim function
  • โœ“ Strip trailing whitespace padding
  • โœ“ Clean product description strings