pysparkpy4j.Py4JException:Methodand([classjava.lang.Integer])不存在

有人可以帮助我了解以下错误吗,我是 PySpark 的新手,开始学习了。

当我用谷歌搜索时,出现以下错误,当我们比较不同类型的数据类型时,我确实有一个名为salary的列作为整数列?为什么我仍然收到此错误。

>>> df.printSchema()
root
 |-- Firstname: string (nullable = true)
 |-- middlename: string (nullable = true)
 |-- lastname: string (nullable = true)
 |-- dob: string (nullable = true)
 |-- sex: string (nullable = true)
 |-- salary: integer (nullable = true)
 |-- CopiedColumn: integer (nullable = true)
 |-- Country: string (nullable = false)
 |-- anotherColumn: string (nullable = false)

>>> df.show()
+---------+----------+--------+----------+---+------+------------+-------+-------------+
|Firstname|middlename|lastname|       dob|sex|salary|CopiedColumn|Country|anotherColumn|
+---------+----------+--------+----------+---+------+------------+-------+-------------+
|    James|          |   Smith|1991-04-01|  M|300000|     -300000|  India|Another value|
|  Michael|      Rose|        |2000-05-19|  M|400000|     -400000|  India|Another value|
|   Robert|          |Williams|1978-09-05|  M|400000|     -400000|  India|Another value|
|    Maria|      Anne|   Jones|1967-12-01|  F|400000|     -400000|  India|Another value|
|      Jen|      Mary|   Brown|1980-02-17|  F|  -100|         100|  India|Another value|
+---------+----------+--------+----------+---+------+------------+-------+-------------+


>>> df.withColumn("lit_value2", when(col("salary") >=400000 & col("salary") <= 500000,lit("100")).otherwise(lit("200"))).show()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/mapr/spark/spark/python/pyspark/sql/column.py", line 115, in _
    njc = getattr(self._jc, name)(jc)
  File "/opt/mapr/spark/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/opt/mapr/spark/spark/python/pyspark/sql/utils.py", line 63, in deco
    return f(*a, **kw)
  File "/opt/mapr/spark/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 332, in get_return_value
py4j.protocol.Py4JError: An error occurred while calling o138.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)

回答

您需要将条件括在括号中:

when((col("salary") >= 400000) & (col("salary") <= 500000), lit("100"))

否则,由于运算符优先级 -&高于>=.

col("salary") >= (400000 & col("salary")) <= 500000

这是没有意义的,并给出了你得到的错误。


以上是pysparkpy4j.Py4JException:Methodand([classjava.lang.Integer])不存在的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>