PySpark:当具有多个输出功能时 [英] PySpark: when function with multiple outputs

查看:82
本文介绍了PySpark:当具有多个输出功能时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用链接时间"功能. 换句话说,我想获得两个以上的输出.

I am trying to use a "chained when" function. In other words, I'd like to get more than two outputs.

我尝试在Excel中使用串联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))

但是那不起作用,因为我无法将元组放入否则"函数中.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆