pyspark.sql.functions.lit#
- pyspark.sql.functions.lit(col)[source]#
- Creates a - Columnof literal value.- New in version 1.3.0. - Changed in version 3.4.0: Supports Spark Connect. - Parameters
- colColumn, str, int, float, bool or list, NumPy literals or ndarray.
- the value to make it as a PySpark literal. If a column is passed, it returns the column as is. - Changed in version 3.4.0: Since 3.4.0, it supports the list type. 
 
- col
- Returns
- Column
- the literal instance. 
 
 - Examples - Example 1: Creating a literal column with an integer value. - >>> import pyspark.sql.functions as sf >>> df = spark.range(1) >>> df.select(sf.lit(5).alias('height'), df.id).show() +------+---+ |height| id| +------+---+ | 5| 0| +------+---+ - Example 2: Creating a literal column from a list. - >>> import pyspark.sql.functions as sf >>> spark.range(1).select(sf.lit([1, 2, 3])).show() +--------------+ |array(1, 2, 3)| +--------------+ | [1, 2, 3]| +--------------+ - Example 3: Creating a literal column from a string. - >>> import pyspark.sql.functions as sf >>> df = spark.range(1) >>> df.select(sf.lit("PySpark").alias('framework'), df.id).show() +---------+---+ |framework| id| +---------+---+ | PySpark| 0| +---------+---+ - Example 4: Creating a literal column from a boolean value. - >>> import pyspark.sql.functions as sf >>> df = spark.createDataFrame([(True, "Yes"), (False, "No")], ["flag", "response"]) >>> df.select(sf.lit(False).alias('is_approved'), df.response).show() +-----------+--------+ |is_approved|response| +-----------+--------+ | false| Yes| | false| No| +-----------+--------+