无法在PySp中创建Dataframe

2024-03-29 09:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用下面的代码在PySpark中创建一个数据帧

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 can not accept object 'Description1323' in type type 'str'

我的代码怎么了?在


Tags: 代码fromimportidfalsesqlschematype
1条回答
网友
1楼 · 发布于 2024-03-29 09:41:27

问题是您正在传递一个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()

结果是:

^{pr2}$

相关问题 更多 >