pyspark.sql.DataFrame.selectExpr

DataFrame.selectExpr(*expr: Union[str, List[str]]) → pyspark.sql.dataframe.DataFrame[source]

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

This is a variant of select() that accepts SQL expressions.

New in version 1.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Returns
DataFrame

A DataFrame with new/old columns transformed by expressions.

Examples

>>> 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|
+---------+--------+