Python PySpark:当具有多个输出时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42537051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:52:17  来源:igfitidea点击:

PySpark: when function with multiple outputs

pythonapache-sparkpysparkpyspark-sql

提问by Fede

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

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

I tried using the same logic of the concatenate IF function in Excel:

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

  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.

但这不起作用,因为我无法将元组放入“otherwise”函数中。

回答by Grr

Have you tried:

你有没有尝试过:

from pyspark.sql import functions as F
df.withColumn('device_id', F.when(col('device')=='desktop', 1).when(col('device')=='mobile', 2).otherwise(None))

Note that when chaining whenfunctions you do not need to wrap the successive calls in an otherwisefunction.

请注意,当链接when函数时,您不需要将连续调用包装在一个otherwise函数中。