PySpark:当函数具有多个输出时

2024-06-30 15:10:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用“链接的时间”函数。 换句话说,我希望得到两个以上的输出。

我尝试在Excel中使用concatenate IF函数的相同逻辑:

  df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))

但这不起作用,因为我不能把元组放入“否则”函数中。


Tags: 函数iddfif链接device时间col
1条回答
网友
1楼 · 发布于 2024-06-30 15:10:03

你试过了吗:

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函数中。

相关问题 更多 >