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)
Source: stackoverflow.com
Related Query
- Appending Row to DataFrame in case of nested Schema in scala
- How to convert Row of a Scala DataFrame into case class most efficiently?
- Replace value in deep nested schema Scala Spark Dataframe
- Add new column to dataframe with modified schema or drop nested columns of array type in Scala
- Extracting row tag schema from StructType in Scala to parse nested XML
- Scala Spark - copy data from 1 Dataframe into another DF with nested schema & same column names
- how to map each row of a scala dataframe to a new schema
- Spark scala creating dataFrame from rdd using Row and Schema
- Provide schema while reading csv file as a dataframe in Scala Spark
- Can I get a Scala case class definition from an Avro schema definition?
- How to specify schema for CSV file without using Scala case class?
- Spark: Programmatically creating dataframe schema in scala
- How to match Dataframe column names to Scala case class attributes?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- Re-using A Schema from JSON within a Spark DataFrame using Scala
- Scala pattern match is not exhaustive on nested case classes
- Convert all keys in a nested map to camel case in Scala
- Flatten nested json in Scala Spark Dataframe
- How to write a DataFrame schema to file in Scala
- Unable to create dataframe from RDD of Row using case class
- make avro schema from a dataframe - spark - scala
- Schema definition in Squeryl using Scala Case Classes
- Scala syntax how to create an instance of a nested case class
- create a spark dataframe from a nested json file in scala
- Change Data Types for Dataframe by Schema in Scala Spark
- Scala nested case class self-bounding inheritance
- Flatten any nested json string and convert to dataframe using spark scala
- How to change the schema of a DataFrame (to fix the names of some nested fields)?
- parsing a large multi nested json into scala case class
- Spark Dataframe schema definition using reflection with case classes and column name aliases
More Query from same tag
- After upgrading to the latest version : org.specs2.execute.Failure required: T
- scala: Error occurred during initialization of VM on Ubuntu 12.04
- LUB of Seq and Set
- use case class in other package in scala
- haskell scala interoperability
- How to flatten an ArrayBuffer in Scala
- Scala: ObjectMapper not able to map JSON to member class
- stop spark Streaming context kafkaDirectStream
- Transform a list of object to lists of its field
- How to redirect AWS sdk logging output
- Scala fast way to parallelize collection
- .r vs r in scala regex
- custom serialization in kafka using scala
- Run scala *.jar file from Ubuntu terminal
- Scala: value class X is added to the return type of its methods as X#
- How to provide an array of values as an env variable to typesafe/lightbend config?
- How to exclude java source files in doc task?
- How to do the Scala's Seq ++ operand in Java
- Why does scala.util.Success.flatMap (Try.flatMap) use try-catch again after applying its argument?
- How to help Scalaz with type inference and 2 type parameters
- How to add context bound TypeTags to types that take type parameters?
- If I have a function take multiple parameters, but I have only a list, how to pass it to the fucntion?
- Map List of tuples in Scala
- How to handle an exception to parsing json in scala
- Pattern matching loses type bounds?
- Is it possible to match on a function's return type in Scala?
- Converting time using Timezone with JodaTime / scala
- See annotations in Scala reflection
- Akka Actor Selection
- How to remove duplicates from a list then sort by most frequent