Explode column with array of arrays - PySpark

Viewed 1391

I have a column with data like this:

[[[-77.1082606, 38.935738]] ,Point] 

I want it split out like:

  column 1          column 2        column 3
 -77.1082606      38.935738           Point

How is that possible using PySpark, or alternatively Scala (Databricks 3.0)? I know how to explode columns but not split up these structs. Thanks!!!

EDIT: Here is the schema of the column:

|-- geometry: struct (nullable = true)
 |    |-- coordinates: string (nullable = false)
 |    |-- type: string (nullable = false
1 Answers

You can use regexp_replace() to get rid of the square brackets, and then split() the resulting string by the comma into separate columns.

from pyspark.sql.functions import regexp_replace, split, col

df.select(regexp_replace(df.geometry.coordinates, "[\[\]]", "").alias("coordinates"),
          df.geometry.type.alias("col3")) \
  .withColumn("arr", split(col("coordinates"), "\\,")) \
  .select(col("arr")[0].alias("col1"),
          col("arr")[1].alias("col2"),
         "col3") \
  .drop("arr") \
  .show(truncate = False)
+-----------+----------+-----+
|col1       |col2      |col3 |
+-----------+----------+-----+
|-77.1082606| 38.935738|Point|
+-----------+----------+-----+
Related