gpt4 book ai didi

python - 如何在 pyspark.sql.functions.when() 中使用多个条件?

转载 作者:IT老高 更新时间:2023-10-28 20:50:39 57 4
gpt4 key购买 nike

我有一个包含几列的数据框。现在我想从其他 2 列派生一个新列:

from pyspark.sql import functions as F
new_df = df.withColumn("new_col", F.when(df["col-1"] > 0.0 & df["col-2"] > 0.0, 1).otherwise(0))

这样我只得到一个异常(exception):

py4j.Py4JException: Method and([class java.lang.Double]) does not exist

它只适用于这样的一个条件:

new_df = df.withColumn("new_col", F.when(df["col-1"] > 0.0, 1).otherwise(0))

有人知道使用多个条件吗?

我使用的是 Spark 1.4。

最佳答案

使用括号强制执行所需的运算符优先级:

F.when( (df["col-1"]>0.0) & (df["col-2"]>0.0), 1).otherwise(0)

关于python - 如何在 pyspark.sql.functions.when() 中使用多个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151861/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com