Reputation: 654
I am trying to create a new dataframe with ArrayType()
column, I tried with and without defining schema but couldn't get the desired result. My code below with schema
from pyspark.sql.types import *
l = [[1,2,3],[3,2,4],[6,8,9]]
schema = StructType([
StructField("data", ArrayType(IntegerType()), True)
])
df = spark.createDataFrame(l,schema)
df.show(truncate = False)
This gives error:
ValueError: Length of object (3) does not match with length of fields (1)
Desired output:
+---------+
|data |
+---------+
|[1,2,3] |
|[3,2,4] |
|[6,8,9] |
+---------+
Edit:
I found a strange thing(atleast for me):
if we use the following code, it gives the expected result:
import pyspark.sql.functions as f
data = [
('person', ['john', 'sam', 'jane']),
('pet', ['whiskers', 'rover', 'fido'])
]
df = spark.createDataFrame(data, ["type", "names"])
df.show(truncate=False)
This gives the following expected output:
+------+-----------------------+
|type |names |
+------+-----------------------+
|person|[john, sam, jane] |
|pet |[whiskers, rover, fido]|
+------+-----------------------+
But if we remove the first column, then it gives unexpected result.
import pyspark.sql.functions as f
data = [
(['john', 'sam', 'jane']),
(['whiskers', 'rover', 'fido'])
]
df = spark.createDataFrame(data, ["names"])
df.show(truncate=False)
This gives the following output:
+--------+-----+----+
|names |_2 |_3 |
+--------+-----+----+
|john |sam |jane|
|whiskers|rover|fido|
+--------+-----+----+
Upvotes: 4
Views: 9071
Reputation: 1353
I think you already have the answer to your question. Another solution is:
>>> l = [([1,2,3],), ([3,2,4],),([6,8,9],)]
>>> df = spark.createDataFrame(l, ['data'])
>>> df.show()
+---------+
| data|
+---------+
|[1, 2, 3]|
|[3, 2, 4]|
|[6, 8, 9]|
+---------+
or
>>> from pyspark.sql.functions import array
>>> l = [[1,2,3],[3,2,4],[6,8,9]]
>>> df = spark.createDataFrame(l)
>>> df = df.withColumn('data',array(df.columns))
>>> df = df.select('data')
>>> df.show()
+---------+
| data|
+---------+
|[1, 2, 3]|
|[3, 2, 4]|
|[6, 8, 9]|
+---------+
Regarding the strange thing, it is not that strange but you need to keep in mind that the tuple with a single value is the single value itself
>>> (['john', 'sam', 'jane'])
['john', 'sam', 'jane']
>>> type((['john', 'sam', 'jane']))
<class 'list'>
so the createDataFrame
sees a list not the tuple.
Upvotes: 5