Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add new columns to DataFrame given their names when they are missing?

I'd like to add selected columns to a DataFrame that are not available already.

val columns=List("Col1","Col2","Col3") 
for(i<-columns) 
 if(!df.schema.fieldNames.contains(i)==true)
 df.withColumn(i,lit(0))

When select column the data frame only old column are coming, new columns are not coming.

like image 698
sks Avatar asked Apr 18 '17 09:04

sks


2 Answers

It's more about how to do it in Scala than Spark and is excellent case for foldLeft (my favorite!)

// start with an empty DataFrame, but could be anything
val df = spark.emptyDataFrame
val columns = Seq("Col1", "Col2", "Col3")
val columnsAdded = columns.foldLeft(df) { case (d, c) =>
  if (d.columns.contains(c)) {
    // column exists; skip it
    d
  } else {
    // column is not available so add it
    d.withColumn(c, lit(0))
  }
}

scala> columnsAdded.printSchema
root
 |-- Col1: integer (nullable = false)
 |-- Col2: integer (nullable = false)
 |-- Col3: integer (nullable = false)
like image 163
Jacek Laskowski Avatar answered Nov 17 '22 19:11

Jacek Laskowski


You can also put the column expressions in a sequence and use star expansion:

val df = spark.range(10)

// Filter out names
val names = Seq("col1", "col2", "col3").filterNot(df.schema.fieldNames.contains)

// Create columns
val cols = names.map(lit(0).as(_))

// Append the new columns to the existing columns.
df.select($"*" +: cols: _*)
like image 39
Herman van Hovell Avatar answered Nov 17 '22 19:11

Herman van Hovell