【问题标题】:Cannot convert a list of int + array(int) into a pyspark dataframe无法将 int + array(int) 列表转换为 pyspark 数据帧
【发布时间】:2021-12-26 04:48:49
【问题描述】:

我正在尝试将 int ID 和 3 个 int 数组转换为具有 2 列的数据帧,然后与 pyspark 中的另一个数据帧联合;

但是,我只是在与架构相关的错误之后遇到错误,并且似乎没有任何效果。我不确定这是为什么。

emp_rdd = spark.sparkContext.emptyRDD()
schema = StructType([
    StructField("id", IntegerType(), True),
    StructField("data", ArrayType(IntegerType()), True),])
df = spark.createDataFrame(data=emp_rdd, schema=schema)

columns = ['id','data']
for i in range(10):     
  data = [id, data1]
  newRows = spark.createDataFrame(data,columns) 
  df= df.union(newRows)

这给了我这个错误;

无法推断类型的架构:

任何帮助将不胜感激

【问题讨论】:

    标签: python dataframe apache-spark pyspark


    【解决方案1】:

    您收到此错误的原因是,在您的 for 循环中,您将参数 data 作为简单列表传递,而 spark.createDataFrame 需要一个可迭代的列表或元组。

    尝试将其更改为:

    data = [(id, data1)]
    

    例子:

    for i in range(5):
        data = [(i, [i + 1, i + 2, i + 3])]
        newRows = spark.createDataFrame(data, columns)
        df = df.union(newRows)
    
    df.show()
    
    #+---+---------+
    #| id|     data|
    #+---+---------+
    #|  0|[1, 2, 3]|
    #|  1|[2, 3, 4]|
    #|  2|[3, 4, 5]|
    #|  3|[4, 5, 6]|
    #|  4|[5, 6, 7]|
    #+---+---------+
    

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多