'Scala , Dataframe column with a list of values, I want to create each value as new column & want to name it

I have a dataframe column given below.

House_No = INT
family_details = ["name" , age , "surname" , weight]
Ownership = Boolean

I want to create new column to the dataframe with name, age, surname & weight.

House_No
family_details
Ownership
name
age
surname
weight


Solution 1:[1]

Below solution will help you:

     val data =  Array((2,Array("abc","23","xyz","70"),true),(3,Array("lmn","45","pqr","50"),false))

     val rdd = sc.parallelize(data)

     val df = rdd.toDF("house_no","family_details","ownership")

val res = df.select("house_no","ownership","family_details").withColumn("name", split($"family_details" (0), ",")(0)).withColumn("age", split($"family_details"(1), ",")(0)).withColumn("surmname", split($"family_details"(2), ",")(0)).withColumn("Weight", split($"family_details"(3), ",")(0)).drop("family_details")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1