【问题标题】:Py4JError: An error occurred while calling o230.andPy4JError:调用 o230.and 时发生错误
【发布时间】:2020-12-28 10:30:59
【问题描述】:

谁能帮助解决这个错误?我正在 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)) - coalesce(data["val"],lit(0)))))

【问题讨论】:

    标签: apache-spark pyspark apache-spark-sql coalesce py4j


    【解决方案1】:

    您需要将条件括在括号中,否则它将解释为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
            )
        )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-21
      • 2019-08-24
      • 2019-06-29
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多