Py4JError:调用 o230.and

问题描述

有人可以帮忙解决这个错误吗?我正在 Pyspark 中编程,我正在尝试使用以下代码计算某个偏差:

Result =   data.select(count(((coalesce(data["pred"],lit(0)))!=0 & (coalesce(data["val"],lit(0)) !=0
& (abs(coalesce(data["pred"],lit(0)) - coalesce(data["val"],lit(0)))/(coalesce(data["val"],lit(0)))) > 0.1))))

出现以下错误

"Py4JError: An error occurred while calling o230.and. Trace:
py4j.Py4JException: Method and([class java.lang.Integer]) does not exist 
at py4j.reflection.ReflectionEngine.getmethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getmethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)"

我对使用 pyspark 编程非常陌生,根本无法发现我的代码有什么问题;我用非常相似的代码做了一个非常相似的计算,但效果很好……有人知道这个问题吗?

PS 这段代码一个不同的计算,具有相似的语法:

Abs_avg = data.select(avg(abs(coalesce(data["pred"],lit(0)))))

解决方法

条件需要用括号括起来,否则会解释为0 & something。此外,您不需要将 ... 包裹在 (...) != 0 中。

Result = data.select(
    count(
        (coalesce(data["pred"],lit(0)) != 0) & 
        (coalesce(data["val"],lit(0)) != 0) & 
        (abs(
             coalesce(data["pred"],lit(0)) - 
             coalesce(data["val"],lit(0))
            ) / coalesce(data["val"],lit(0)) > 0.1
        )
    )
)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...