<分区>
我正在尝试使用“链接时间”函数。 换句话说,我想要获得两个以上的输出。
我尝试在 Excel 中使用与连接 IF 函数相同的逻辑:
df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))
但这不起作用,因为我不能将元组放入“otherwise”函数中。
<分区>
我正在尝试使用“链接时间”函数。 换句话说,我想要获得两个以上的输出。
我尝试在 Excel 中使用与连接 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/