这个问题已经有了答案:
Spark Equivalent of IF Then ELSE
4个答案
我尝试使用“chained when”函数。
换句话说,我想得到两个以上的输出。
我尝试使用与Excel中concatenate if函数相同的逻辑:
df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))
但这不起作用,因为我不能将元组放入“otherwise”函数中。
最佳答案
您是否尝试过:
from pyspark.sql import functions as F
df.withColumn('device_id', F.when(col('device')=='desktop', 1).when(col('device')=='mobile', 2).otherwise(None))
注意,当链接
when
函数时,不需要将连续调用包装在otherwise
函数中。关于python - PySpark:具有多个输出的函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42537051/