Skip to main content
PYSPARK • LESSON 53

Right Trimming with rtrim()

How do we remove trailing spaces from product description fields?

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