我有一个包含几列的数据框。现在我想从其他 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/