If statement 如何在PySpark中执行嵌套操作?
大家好,我正在尝试解释这个PowerBi语法并将其转换为Pyspark 这就是我所尝试的: 但是,我有一个错误:If statement 如何在PySpark中执行嵌套操作?,if-statement,pyspark,case-when,If Statement,Pyspark,Case When,大家好,我正在尝试解释这个PowerBi语法并将其转换为Pyspark 这就是我所尝试的: 但是,我有一个错误: 非常感谢您提供的任何帮助,请举个例子说明@jxc的意思: 假设您已经有一个名为df的数据帧: from pyspark.sql.functions import expr Intensities = df.withColumn('Intensities', expr("CASE WHEN Intensity = 'Very High' AND Severity = 'Ve
非常感谢您提供的任何帮助,请举个例子说明@jxc的意思: 假设您已经有一个名为df的数据帧:
from pyspark.sql.functions import expr
Intensities = df.withColumn('Intensities', expr("CASE WHEN Intensity = 'Very High' AND Severity = 'Very High' THEN 'Red' WHEN .... ELSE ... END"))
我把“…”作为占位符放进去,但我认为它清楚地表明了方法。尝试将嵌入的
if
s逻辑转换为嵌入的SQL的case/when语句,然后使用f.expr()
函数检索结果。
Intensities = df.withColumn(('Intensities',f.when((f.col('Intensity') == 'Very High') & (f.col('Severity') == 'Very High') , "Red").
otherwise(f.when((f.col('Intensity') == 'Very High') & (f.col('Severity') == 'High') , "Red").
otherwise(f.when((f.col('Intensity') == 'Very High') & (f.col('Severity') == 'Medium') , "Orange")
.otherwise('Yellow'))))
.otherwise(f.when((f.col('Intensity') == 'High') & (f.col('Severity') == 'Very High') , "Red").
otherwise(f.when((f.col('Intensity') == 'High') & (f.col('Severity') == 'High') , "Orange").
otherwise(f.when((f.col('Intensity') == 'High') & (f.col('Severity') == 'Medium') , "Orange")
.otherwise('Yellow'))))
.otherwise(f.when((f.col('Intensity') == 'Medium') & (f.col('Severity') == 'Very High') , "Orange").
otherwise(f.when((f.col('Intensity') == 'Medium') & (f.col('Severity') == 'High') , "Yellow").
otherwise(f.when((f.col('Intensity') == 'Medium') & (f.col('Severity') == 'Medium') , "Yellow")
.otherwise('Green'))))
.otherwise(f.when((f.col('Intensity') == 'Low') & (f.col('Severity') == 'Very High') , "Yellow").
otherwise(f.when((f.col('Intensity') == 'Low') & (f.col('Severity') == 'High') , "Green").
otherwise(f.when((f.col('Intensity') == 'Low') & (f.col('Severity') == 'Medium') , "Green")
.otherwise('Green'))))
).otherwise("")
A Tuple Object dosen't have an attribute Otherwise
from pyspark.sql.functions import expr
Intensities = df.withColumn('Intensities', expr("CASE WHEN Intensity = 'Very High' AND Severity = 'Very High' THEN 'Red' WHEN .... ELSE ... END"))