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
PySpark: when function with multiple outputs
提问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 when
functions you do not need to wrap the successive calls in an otherwise
function.
请注意,当链接when
函数时,您不需要将连续调用包装在一个otherwise
函数中。