lit()
function in Spark is used to create a new column with a constant or literal value. It is part of the pyspark.sql.functions
module and is particularly useful when you need to add a column with a fixed value to a DataFrame. This function is often used in combination with other transformations, such as withColumn()
.
Column
object representing the constant value.lit()
in an Expressionlit()
with Null Valueslit()
with Conditional Logiclit()
with String Concatenationlit()
with Date and Timestamp Valueslit()
is a metadata operation and does not involve data movement, making it very efficient.lit()
with other functions (e.g., withColumn()
, select()
) for advanced transformations.lit()
function is used to create a new column with a constant or literal value.SELECT
statements.lit()
is lightweight and does not impact performance.