How to explode an array into multiple columns in Spark

lserlohn

I have a spark dataframe looks like:

id   DataArray
a    array(3,2,1)
b    array(4,2,1)     
c    array(8,6,1)
d    array(8,2,4)

I want to transform this dataframe into:

id  col1  col2  col3
a    3     2     1
b    4     2     1
c    8     6     1 
d    8     2     4

What function should I use?

user9554572

Use apply:

import org.apache.spark.sql.col

df.select(
  col("id") +: (0 until 3).map(i => col("DataArray")(i).alias(s"col$i")): _*
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to explode an array into multiple columns in Spark Java

How to explode two array fields to multiple columns in Spark?

explode an spark array column to multiple columns sparksql

Explode array into columns Spark

Spark: explode multiple columns into one

Independently explode multiple columns in Spark

How to explode column with multiple records into multiple Columns in Spark

Spark explode array column to columns

How to explode each row that is an Array into columns in Spark (Scala)?

Spark explode multiple columns of row in multiple rows

Zip and Explode multiple Columns in Spark SQL Dataframe

Explode multiple columns in Spark SQL table

Explode (transpose?) multiple columns in Spark SQL table

Scala Spark Explode multiple columns pairs into rows

Explode multiple array columns with variable lengths

Spark: How to transpose and explode columns with nested arrays

Spark - explode and merge columns

How to explode multiple columns of a dataframe in pyspark

How to explode an array column in spark java with dataset

Explode a nested array into new columns using Java Spark

Explode multiple columns

Explode on multiple columns in Hive

Explode multiple columns in Pandas

Spark: How to transpose and explode columns with dynamic nested arrays

How to explode in spark with delimiter

how to explode a spark dataframe

How to explode a one row Spark dataframe in JSON format to multiple rows?

How to explode multiple columns, different types and different lengths?

How to explode Spark dataframe Array field with Unique identifiers in Scala?