运行python文件时出错:列数不匹配。\ n旧列名(1):_ c0 \ n新列名(4)

当我运行以下代码时,出现了一些错误,提示列数不匹配。\ n旧列名(1):_ c0 \ n新列名(4):由于我是python的新手,我想知道如何解决此问题:

from pyspark import SparkContext
from pyspark.sql import SparkSession
from pyspark.sql import functions as F

# create Spark context and session with necessary configuration
sc = SparkContext('local[1]','2_program_2a_avg')
spark = SparkSession(sc)

# read as dataframe with using delimiter
df = spark.read \
     .format('csv') \
     .options(header='false',delimiter=' ') \
     .load('/home/hduser/Desktop/sample1/avg/input_avg.csv') \
     .toDF('ownerid','houseid','zipcode','value')

# select the necessary columns
df = df.select('zipcode','value')

# compute the average house value of each zipcode
df = df.groupBy('zipcode').agg(F.mean('value'),F.count('value')).orderBy('avg(value)',ascending=True)

# show the result
df.show()

# change it to rdd so that we can save the output as text file
df = df.rdd.saveAsTextFile('/home/hduser/Desktop/sample1/avg/')

# stop spark context
sc.stop()

输出

*/home/hduser/PycharmProjects/Assignment2/venv/bin/python /home/hduser/PycharmProjects/HelloWorld/avg_value.py
    19/11/12 10:47:49 WARN Utils: Your hostname,hadoop resolves to a loopback address: 127.0.1.1; using 10.0.2.15 instead (on interface enp0s3)
    19/11/12 10:47:49 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
    19/11/12 10:47:51 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
    Setting default log level to "WARN".
    To adjust logging level use sc.setLogLevel(newLevel). For SparkR,use setLogLevel(newLevel).
    Traceback (most recent call last):
      File "/opt/spark/python/pyspark/sql/utils.py",line 63,in deco
        return f(*a,**kw)
      File "/home/hduser/PycharmProjects/Assignment2/venv/lib/python3.6/site-packages/py4j/protocol.py",line 328,in get_return_value
        format(target_id,".",name),value)
    py4j.protocol.Py4JJavaError: An error occurred while calling o28.toDF.
    : java.lang.IllegalArgumentException: requirement failed: The number of columns doesn't match.
    Old column names (1): _c0
    New column names (4): ownerid,houseid,zipcode,value
        at scala.Predef$.require(Predef.scala:224)
        at org.apache.spark.sql.Dataset.toDF(Dataset.scala:447)
        at sun.reflect.NativeMethodaccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodaccessorImpl.invoke(NativeMethodaccessorImpl.java:62)
        at sun.reflect.DelegatingMethodaccessorImpl.invoke(DelegatingMethodaccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
        at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
        at py4j.Gateway.invoke(Gateway.java:282)
        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)


    During handling of the above exception,another exception occurred:

    Traceback (most recent call last):
      File "/home/hduser/PycharmProjects/HelloWorld/avg_value.py",line 15,in <module>
        .toDF(*columns)
      File "/opt/spark/python/pyspark/sql/dataframe.py",line 2055,in toDF
        jdf = self._jdf.toDF(self._jseq(cols))
      File "/home/hduser/PycharmProjects/Assignment2/venv/lib/python3.6/site-packages/py4j/java_gateway.py",line 1257,in __call__
        answer,self.gateway_client,self.target_id,self.name)
      File "/opt/spark/python/pyspark/sql/utils.py",line 79,in deco
        raise IllegalArgumentException(s.split(': ',1)[1],stackTrace)
    pyspark.sql.utils.IllegalArgumentException: "requirement failed: The number of columns doesn't match.\nOld column names (1): _c0\nNew column names (4): ownerid,value"

    Process finished with exit code 1*
zhouanneng 回答:运行python文件时出错:列数不匹配。\ n旧列名(1):_ c0 \ n新列名(4)

我刚刚修改了一行代码,现在可以正常工作了。

df = spark.read \
 .format('csv') \
 .options(header='true').load('/home/hduser/Desktop/sample1/avg/input_avg.csv')
本文链接:https://www.f2er.com/3120241.html

大家都在问