score:1

package spark

import org.apache.spark.sql.sparksession
import org.apache.spark.sql.functions.col

object arraytocol extends app {
  val spark = sparksession.builder()
    .master("local")
    .appname("dataframe-example")
    .getorcreate()

  import spark.implicits._
  val inptdf = seq(seq("a", "b", "c"), seq("x", "y", "z")).todf("value")
  val d = inptdf
    .withcolumn("0", col("value").getitem(0))
    .withcolumn("1", col("value").getitem(1))
    .withcolumn("2", col("value").getitem(2))
    .drop("value")

  d.show(false)

}
// variant 2
val res = inptdf.select(
    $"value".getitem(0).as("col0"),
    $"value".getitem(1).as("col1"),
    $"value".getitem(2).as("col2")
  )
// variant 3
val res1 = inptdf.select(
    col("*") +: (0 until 3).map(i => col("value").getitem(i).as(s"$i")): _*
  )
    .drop("value")

score:1

another way to create a dataframe ---

df1 = spark.createdataframe([(1,[4,2, 1]),(4,[3,2])], [ "col2","col4"])

output---------

+----+---------+
|col2|     col4|
+----+---------+
|   1|[4, 2, 1]|
|   4|   [3, 2]|
+----+---------+

Related Query