1
votes

My data frame looks like -

+----+----+-------------+
|col1|col2|         col3|
+----+----+-------------+
|   1|   A|[[[1, 2, 3]]]|
|   2|   B|   [[[3, 5]]]|
+----+----+-------------+

I want data frame -

+----+----+----+
|col1|col2|col3|
+----+----+----+
|   1|   A|   1|
|   1|   A|   2|
|   1|   A|   3|
|   2|   B|   3|
|   2|   B|   5|
+----+----+----+

My code is like -

from pyspark.sql.functions import explode
df = spark.createDataFrame([(1, "A", [[[1,2,3]]]), (2, "B", [[[3,5]]])],["col1", "col2", "col3"])
df1 = df.withColumn("col3", explode(df.col3))
df1.show()

But the output is -

+----+----+-----------+
|col1|col2|       col3|
+----+----+-----------+
|   1|   A|[[1, 2, 3]]|
|   2|   B|   [[3, 5]]|
+----+----+-----------+

How to solve it using explode function in pyspark

1
df.withColumn('col3', explode(df.col3[0][0])).show() - jxc

1 Answers

0
votes

As you are having nested array we need to flatten nested arrays by using flatten in built function first then use explode function.

Try with:

from pyspark.sql.functions import *
df.withColumn("col3",explode(flatten(flatten(col("col3"))))).show()
#+----+----+----+
#|col1|col2|col3|
#+----+----+----+
#|   1|   A|   1|
#|   1|   A|   2|
#|   1|   A|   3|
#|   2|   B|   3|
#|   2|   B|   5|
#+----+----+----+

Other way would be traversing through nested array[0][0] and do explodeon inner most array as mentioned in comment by jxc.

df.withColumn("col3", explode(col("col3")[0][0])).show()