pyspark.sql.functions.ltrim#
- pyspark.sql.functions.ltrim(col, trim=None)[source]#
Trim the spaces from left end for the specified string value.
New in version 1.5.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- Returns
Column
left 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.ltrim("value")).show() +--------+------------+ | value|ltrim(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.ltrim("value", sf.lit("*"))).show() +--------+--------------------------+ | value|TRIM(LEADING * 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.ltrim("value", "t")).show() +--------+---+--------------------------+ | value| t|TRIM(LEADING t FROM value)| +--------+---+--------------------------+ |**Spark*| *| Spark*| |==Spark=| =| Spark=| +--------+---+--------------------------+