Skip to main content

selectExpr

Projects a set of SQL expressions and returns a new DataFrame.

Syntax

selectExpr(*expr: Union[str, List[str]])

Parameters

Parameter

Type

Description

expr

str or list of str

SQL expression strings to project.

Parameter

Type

Description

expr

str or list of str

SQL expression strings to project.

Returns

DataFrame: A DataFrame with new/old columns transformed by expressions.

Examples

Python
df = spark.createDataFrame([
(2, "Alice"), (5, "Bob")], schema=["age", "name"])
df.selectExpr("age * 2", "abs(age)").show()
# +---------+--------+
# |(age * 2)|abs(age)|
# +---------+--------+
# | 4| 2|
# | 10| 5|
# +---------+--------+