代码之家  ›  专栏  ›  技术社区  ›  Ahmad Senousi

Spark \uu getnewargs\uuuu错误。。。方法或([类java.lang.String])不存在

  •  6
  • Ahmad Senousi  · 技术社区  · 8 年前

    我正在尝试根据列值是否位于另一列中向DataFrame添加列,如下所示:

    df = df.withColumn('new_column', when(df['color']=='blue' | df['color']=='green', 'A').otherwise('WD'))
    

    运行代码后,我得到以下错误:

    Py4JError: An error occurred while calling o59.or. Trace:
    py4j.Py4JException: Method or([class java.lang.String]) 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:214)
        at java.lang.Thread.run(Thread.java:748)
    

    我该如何克服这个问题? 我正在使用PySpark 2.3.0

    2 回复  |  直到 5 年前
        1
  •  19
  •   Suresh    8 年前

    使用多个条件时,由于运算符的优先级,每个条件都需要分开。

    df=df.withColumn('new_column',when((df['color']=='blue')|(df['color']=='green'),'A').otherwise('WD'))
    
        2
  •  0
  •   user13951299    4 年前

    我发现了一个类似的问题 Py4JError: An error occurred while calling o166.and. Trace: 日志指向我的这一行代码:

    .when(df.line_item_line_item_type == 'Fee' & df.reservation_reservation_a_r_n != '', 0)

    在“&”的每一侧的语句周围添加括号集解决了问题,如下所示:

    .when((df.line_item_line_item_type == 'Fee') & (df.reservation_reservation_a_r_n != ''), 0)

    推荐文章