pyspark.sql.functions.trim#
- pyspark.sql.functions.trim(col, trim=None)[source]#
- Trim the spaces from both ends for the specified string column. - New in version 1.5.0. - Changed in version 3.4.0: Supports Spark Connect. - Parameters
- Returns
- Column
- trimmed values from both sides. 
 
 - Examples - Example 1: Trim the spaces - >>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([" Spark", "Spark ", " Spark"], "STRING") >>> df.select("*", sf.trim("value")).show() +--------+-----------+ | value|trim(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.trim("value", sf.lit("*"))).show() +--------+-----------------------+ | value|TRIM(BOTH * 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.trim("value", "t")).show() +--------+---+-----------------------+ | value| t|TRIM(BOTH t FROM value)| +--------+---+-----------------------+ |**Spark*| *| Spark| |==Spark=| =| Spark| +--------+---+-----------------------+