Skip to main content

lit

Creates a Column of literal value. Supports Spark Connect.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.lit(col=<col>)

Parameters

Parameter

Type

Description

col

pyspark.sql.Column, str, int, float, bool, or list

The value to make it as a PySpark literal. If a column is passed, it returns the column as is.

Returns

pyspark.sql.Column: the literal instance.

Examples

Example 1: Creating a literal column with an integer value.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.range(1)
df.select(dbf.lit(5).alias('height'), df.id).show()
Output
+------+---+
|height| id|
+------+---+
| 5| 0|
+------+---+

Example 2: Creating a literal column from a list.

Python
from pyspark.databricks.sql import functions as dbf
spark.range(1).select(dbf.lit([1, 2, 3])).show()
Output
+--------------+
|array(1, 2, 3)|
+--------------+
| [1, 2, 3]|
+--------------+

Example 3: Creating a literal column from a string.

Python
from pyspark.databricks.sql import functions as dbf
df = spark.range(1)
df.select(dbf.lit("PySpark").alias('framework'), df.id).show()
Output
+---------+---+
|framework| id|
+---------+---+
| PySpark| 0|
+---------+---+