【发布时间】:2019-02-11 20:39:43
【问题描述】:
我有一个 pyspark 数据框,我正在尝试使用 toPandas() 将其转换为 pandas,但是我遇到了下面提到的错误。
我尝试了不同的选项,但得到了相同的错误:
1) 将数据限制为仅几条记录
2) 明确使用 collect()(我相信 toPandas() 固有地使用它)
探索了许多关于 SO 的帖子,但 AFAIK 没有一个有 toPandas() 问题。
我的数据框的快照:-
>>sc.version
2.3.0.2.6.5.0-292
>>print(type(df4),len(df4.columns),df4.count(),
(<class 'pyspark.sql.dataframe.DataFrame'>, 13, 296327)
>>df4.printSchema()
root
|-- id: string (nullable = true)
|-- gender: string (nullable = true)
|-- race: string (nullable = true)
|-- age: double (nullable = true)
|-- status: integer (nullable = true)
|-- height: decimal(6,2) (nullable = true)
|-- city: string (nullable = true)
|-- county: string (nullable = true)
|-- zipcode: string (nullable = true)
|-- health: double (nullable = true)
|-- physical_inactivity: double (nullable = true)
|-- exercise: double (nullable = true)
|-- weight: double (nullable = true)
>>df4.limit(2).show()
+------+------+------+----+-------+-------+---------+-------+-------+------+-------------------+--------+------------+
|id |gender|race |age |status |height | city |county |zipcode|health|physical_inactivity|exercise|weight |
+------+------+------+----+-------+-------+---------+-------+-------+------+-------------------+--------+------------+
| 90001| MALE| WHITE|61.0| 0| 70.51|DALEADALE|FIELD | 29671| null| 29.0| 49.0| 162.0|
| 90005| MALE| WHITE|82.0| 0| 71.00|DALEBDALE|FIELD | 36658| 16.0| null| 49.0| 195.0|
+------+------+------+----+-------+-------+---------+-------+-------+------+-------------------+--------+------------+
*had to mask few features due to data privacy concerns
错误:-
>>df4.limit(10).toPandas()
'int' object is not iterable
Traceback (most recent call last):
File "/repo/python2libs/pyspark/sql/dataframe.py", line 1968, in toPandas
pdf = pd.DataFrame.from_records(self.collect(), columns=self.columns)
File "/repo/python2libs/pyspark/sql/dataframe.py", line 467, in collect
return list(_load_from_socket(sock_info, BatchedSerializer(PickleSerializer())))
File "/repo/python2libs/pyspark/rdd.py", line 142, in _load_from_socket
port, auth_secret = sock_info
TypeError: 'int' object is not iterable
【问题讨论】:
-
您能否发布一个产生此错误的数据框示例?
-
@A.Leistra 添加了一个示例,请查看数据框
-
我无法使用此数据框示例复制您的错误,您是否收到此示例的错误?
-
@A.Leistra 是的,即使使用确切的样本,我也会遇到同样的错误。只是想知道是否与Spark版本有关?我可以知道你的火花版本!
标签: pandas apache-spark pyspark apache-zeppelin