PySpark SQL 函数的 expr(~) 方法解析给定的 SQL 表达式。
参数
1. str | string
要解析的 SQL 表达式。
返回值
PySpark 专栏。
例子
考虑以下PySpark DataFrame:
df = spark.createDataFrame([['Alex',30],['Bob',50]], ['name','age'])
df.show()
+----+---+
|name|age|
+----+---+
|Alex| 30|
| Bob| 50|
+----+---+使用 expr 方法将列值转换为大写
expr(~) 方法接受 SQL 表达式作为参数,因此我们可以使用 SQL 函数,例如 upper(~) :
import pyspark.sql.functions as F
df.select(F.expr('upper(name)')).show()
+-----------+
|upper(name)|
+-----------+
|       ALEX|
|        BOB|
+-----------+注意
expr(~) 方法通常可以使用 PySpark DataFrame 的  selectExpr(~)  方法编写得更简洁。例如,上面的情况可以改写为:
df.selectExpr('upper(name)').show()
+-----------+
|upper(name)|
+-----------+
|       ALEX|
|        BOB|
+-----------+我建议您尽可能使用 selectExpr(~) ,因为:
- 
您不必导入 SQL 函数库 ( pyspark.sql.functions)。
- 
语法更短 
使用expr方法解析复杂的SQL表达式
下面是使用 AND 和 LIKE 等子句的更复杂的 SQL 表达式:
df.select(F.expr('age > 40 AND name LIKE "B%"').alias('result')).show()
+------+
|result|
+------+
| false|
|  true|
+------+请注意以下事项:
- 
我们正在检查 age大于40且name以B开头的行。
- 
我们使用 alias(~)方法将标签'result'分配给expr(~)返回的Column。
expr方法返回的布尔掩码的实际应用
正如我们在上面的示例中看到的,expr(~) 方法可以根据您提供的 SQL 表达式返回布尔掩码:
df.select(F.expr('age > 40 AND name LIKE "B%"').alias('result')).show()
+------+
|result|
+------+
| false|
|  true|
+------+这允许我们使用 any(~) 检查是否存在满足给定条件的行:
df.select(F.expr('any(age > 40 AND name LIKE "B%")').alias('exists?')).show()
+-------+
|exists?|
+-------+
|   true|
+-------+在这里,我们得到True,因为布尔掩码中至少存在一个True值。
使用 expr 方法映射列值
我们可以在 expr(~) 方法中使用 CASE WHEN 映射列值,如下所示:
col = F.expr('CASE WHEN age < 40 THEN "JUNIOR" ELSE "SENIOR" END').alias('result')
df.withColumn('status', col).show()
+----+---+------+
|name|age|status|
+----+---+------+
|Alex| 30|JUNIOR|
| Bob| 50|SENIOR|
+----+---+------+在此,请注意以下事项:
- 
我们正在使用 DataFrame 的 withColumn(~)方法来获取新的 PySpark DataFrame,其中包括expr(~)返回的列。
相关用法
- Python PySpark SQL Functions explode方法用法及代码示例
- Python PySpark SQL Functions element_at方法用法及代码示例
- Python PySpark SQL Functions split方法用法及代码示例
- Python PySpark SQL Functions repeat方法用法及代码示例
- Python PySpark SQL Functions concat方法用法及代码示例
- Python PySpark SQL Functions instr方法用法及代码示例
- Python PySpark SQL Functions count_distinct方法用法及代码示例
- Python PySpark SQL Functions dayofmonth方法用法及代码示例
- Python PySpark SQL Functions date_add方法用法及代码示例
- Python PySpark SQL Functions array方法用法及代码示例
- Python PySpark SQL Functions concat_ws方法用法及代码示例
- Python PySpark SQL Functions col方法用法及代码示例
- Python PySpark SQL Functions translate方法用法及代码示例
- Python PySpark SQL Functions dayofweek方法用法及代码示例
- Python PySpark SQL Functions regexp_extract方法用法及代码示例
- Python PySpark SQL Functions regexp_replace方法用法及代码示例
- Python PySpark SQL Functions round方法用法及代码示例
- Python PySpark SQL Functions countDistinct方法用法及代码示例
- Python PySpark SQL Functions date_format方法用法及代码示例
- Python PySpark SQL Functions collect_list方法用法及代码示例
- Python PySpark SQL Functions lit方法用法及代码示例
- Python PySpark SQL Functions upper方法用法及代码示例
- Python PySpark SQL Functions length方法用法及代码示例
- Python PySpark SQL Functions dayofyear方法用法及代码示例
- Python PySpark SQL Functions trim方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 PySpark SQL Functions | expr method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
