pyspark.sql.functions.rtrim#
- pyspark.sql.functions.rtrim(col, trim=None)[source]#
Trim the spaces from right end for the specified string value.
New in version 1.5.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- Returns
Column
right trimmed values.
Examples
Example 1: Trim the spaces
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([" Spark", "Spark ", " Spark"], "STRING") >>> df.select("*", sf.rtrim("value")).show() +--------+------------+ | value|rtrim(value)| +--------+------------+ | Spark| Spark| | Spark | Spark| | Spark| Spark| +--------+------------+
Example 2: Trim specified characters
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame(["***Spark", "Spark**", "*Spark"], "STRING") >>> df.select("*", sf.rtrim("value", sf.lit("*"))).show() +--------+---------------------------+ | value|TRIM(TRAILING * FROM value)| +--------+---------------------------+ |***Spark| ***Spark| | Spark**| Spark| | *Spark| *Spark| +--------+---------------------------+
Example 3: Trim a column containing different characters
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([("**Spark*", "*"), ("==Spark=", "=")], ["value", "t"]) >>> df.select("*", sf.rtrim("value", "t")).show() +--------+---+---------------------------+ | value| t|TRIM(TRAILING t FROM value)| +--------+---+---------------------------+ |**Spark*| *| **Spark| |==Spark=| =| ==Spark| +--------+---+---------------------------+