【问题标题】:Cannot create Dataframe in PySpark无法在 PySpark 中创建数据框
【发布时间】:2018-10-01 07:13:26
【问题描述】:

我想使用以下代码在 PySpark 中创建一个 Dataframe

from pyspark.sql import *
from pyspark.sql.types import *

temp = Row("DESC", "ID")
temp1 = temp('Description1323', 123)

print temp1

schema = StructType([StructField("DESC", StringType(), False),
                     StructField("ID", IntegerType(), False)])

df = spark.createDataFrame(temp1, schema)

但我收到以下错误:

TypeError: StructType 不能接受类型中的对象“Description1323” 输入'str'

我的代码有什么问题?

【问题讨论】:

    标签: python apache-spark pyspark databricks


    【解决方案1】:

    问题是你传递了一个Row,你应该传递一个Rows 的列表。试试这个:

    from pyspark.sql import *
    from pyspark.sql.types import *
    
    temp = Row("DESC", "ID")
    temp1 = temp('Description1323', 123)
    
    print temp1
    
    schema = StructType([StructField("DESC", StringType(), False),
                         StructField("ID", IntegerType(), False)])
    
    df = spark.createDataFrame([temp1], schema)
    
    df.show()
    

    结果:

    +---------------+---+
    |           DESC| ID|
    +---------------+---+
    |Description1323|123|
    +---------------+---+
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 2020-03-14
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 2021-12-10
      相关资源
      最近更新 更多