PySpark:当具有多个输出时 [英] PySpark: when function with multiple outputs
本文介绍了PySpark:当具有多个输出时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用chained when"函数.换句话说,我想得到两个以上的输出.
I am trying to use a "chained when" function. In other words, I'd like to get more than two outputs.
我尝试在 Excel 中使用与 concatenate IF 函数相同的逻辑:
I tried using the same logic of the concatenate IF function in Excel:
df.withColumn("device_id", when(col("device")=="desktop",1)).otherwise(when(col("device")=="mobile",2)).otherwise(null))
但这不起作用,因为我无法将元组放入otherwise"函数中.
But that doesn't work since I can't put a tuple into the "otherwise" function.
推荐答案
您是否尝试过:
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
函数中.
Note that when chaining when
functions you do not need to wrap the successive calls in an otherwise
function.
这篇关于PySpark:当具有多个输出时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文