I have a pyspark dataframe and I am trying to convert it to pandas using toPandas(), however I am running into below mentioned error.
I tried different options but got the same error:
1) limit the data to just few records
2) used collect() explicitly (which I believe toPandas() uses inherently)
Explored many posts on SO, but AFAIK none has toPandas() issue.
Snapshot of my dataframe:-
>>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
Error:-
>>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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…