score:0

Check below code.

scala> df.show(false)
+-------+
|vitamin|
+-------+
|1      |
+-------+

scala> val vitaminActive: List[Int] = List(1,0,1)

scala> df.withColumn("Presents",struct(vitaminActive.map(lit(_)):_*).cast("struct<A:Int,B:Int,C:Int>")).show(false)
+-------+---------+
|vitamin|Presents |
+-------+---------+
|1      |[1, 0, 1]|
+-------+---------+

scala> df.withColumn("Presents",struct(vitaminActive.map(lit(_)):_*).cast("struct<A:Int,B:Int,C:Int>")).printSchema
root
 |-- vitamin: integer (nullable = false)
 |-- Presents: struct (nullable = false)
 |    |-- A: integer (nullable = true)
 |    |-- B: integer (nullable = true)
 |    |-- C: integer (nullable = true)


Related Query

More Query from same tag