gpt4 book ai didi

apache-spark - 从 Row 创建 DataFrame 结果为 'infer schema issue'

转载 作者:行者123 更新时间:2023-12-04 05:15:18 26 4
gpt4 key购买 nike

开始学习PySpark时,我用一个列表创建了一个dataframe .现在从列表中推断模式已被弃用,我收到警告并建议我使用 pyspark.sql.Row反而。但是,当我尝试使用 Row 创建一个时,我得到推断架构问题。这是我的代码:

>>> row = Row(name='Severin', age=33)
>>> df = spark.createDataFrame(row)

这会导致以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/spark2-client/python/pyspark/sql/session.py", line 526, in createDataFrame
rdd, schema = self._createFromLocal(map(prepare, data), schema)
File "/spark2-client/python/pyspark/sql/session.py", line 390, in _createFromLocal
struct = self._inferSchemaFromList(data)
File "/spark2-client/python/pyspark/sql/session.py", line 322, in _inferSchemaFromList
schema = reduce(_merge_type, map(_infer_schema, data))
File "/spark2-client/python/pyspark/sql/types.py", line 992, in _infer_schema
raise TypeError("Can not infer schema for type: %s" % type(row))
TypeError: Can not infer schema for type: <type 'int'>

所以我创建了一个架构

>>> schema = StructType([StructField('name', StringType()), 
... StructField('age',IntegerType())])
>>> df = spark.createDataFrame(row, schema)

但是,这个错误被抛出。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/spark2-client/python/pyspark/sql/session.py", line 526, in createDataFrame
rdd, schema = self._createFromLocal(map(prepare, data), schema)
File "/spark2-client/python/pyspark/sql/session.py", line 387, in _createFromLocal
data = list(data)
File "/spark2-client/python/pyspark/sql/session.py", line 509, in prepare
verify_func(obj, schema)
File "/spark2-client/python/pyspark/sql/types.py", line 1366, in _verify_type
raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj)))
TypeError: StructType can not accept object 33 in type <type 'int'>

最佳答案

createDataFrame函数需要一个 行列表 (在其他选项中)加上模式,所以正确的代码应该是这样的:

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

schema = StructType([StructField('name', StringType()), StructField('age',IntegerType())])
rows = [Row(name='Severin', age=33), Row(name='John', age=48)]
df = spark.createDataFrame(rows, schema)

df.printSchema()
df.show()

出去:
root
|-- name: string (nullable = true)
|-- age: integer (nullable = true)

+-------+---+
| name|age|
+-------+---+
|Severin| 33|
| John| 48|
+-------+---+

在 pyspark 文档 ( link ) 中,您可以找到有关 createDataFrame 函数的更多详细信息。

关于apache-spark - 从 Row 创建 DataFrame 结果为 'infer schema issue',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44948465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com