pyspark.sql.functions.lpad#
- pyspark.sql.functions.lpad(col, len, pad)[source]#
Left-pad the string column to width len with pad.
Added in version 1.5.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters:
- col
Columnor column name target column to work on. A column that evaluates to a string or binary.
- len
Columnor int length of the final string. A column that evaluates to an integer.
Changed in version 4.0.0: pattern now accepts column.
- pad
Columnor literal string chars to prepend. A column that evaluates to a string or binary.
Changed in version 4.0.0: pattern now accepts column.
- col
- Returns:
Columnleft padded result. Returns a column of the same type as the input.
See also
Examples
Example 1: Pad with a literal string
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([('abcd',), ('xyz',), ('12',)], ['s',]) >>> df.select("*", sf.lpad(df.s, 6, '#')).show() +----+-------------+ | s|lpad(s, 6, #)| +----+-------------+ |abcd| ##abcd| | xyz| ###xyz| | 12| ####12| +----+-------------+
Example 2: Pad with a bytes column
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([('abcd',), ('xyz',), ('12',)], ['s',]) >>> df.select("*", sf.lpad(df.s, 6, sf.lit(b"uv"))).show() +----+-------------------+ | s|lpad(s, 6, X'7576')| +----+-------------------+ |abcd| uvabcd| | xyz| uvuxyz| | 12| uvuv12| +----+-------------------+