Spark 2.2 +
Spark 2.2 introduces typedLit to support Seq , Map and Tuples ( SPARK-19254 ), and the following calls (Scala) must be supported:
import org.apache.spark.sql.functions.typedLit df.withColumn("some_array", typedLit(Seq(1, 2, 3))) df.withColumn("some_struct", typedLit(("foo", 1, .0.3))) df.withColumn("some_map", typedLit(Map("key1" -> 1, "key2" -> 2)))
Spark 1. 3+ ( lit ), 1. 4+ ( array , struct ), 2. 0+ ( map ):
The second argument to DataFrame.withColumn should be Column so you should use a literal:
from pyspark.sql.functions import lit df.withColumn('new_column', lit(10))
If you need complex columns, you can build them using blocks like array :
from pyspark.sql.functions import array, create_map, struct df.withColumn("some_array", array(lit(1), lit(2), lit(3))) df.withColumn("some_struct", struct(lit("foo"), lit(1), lit(.3))) df.withColumn("some_map", create_map(lit("key1"), lit(1), lit("key2"), lit(2)))
Exactly the same methods can be used in Scala.
import org.apache.spark.sql.functions.{array, lit, map, struct} df.withColumn("new_column", lit(10)) df.withColumn("map", map(lit("key1"), lit(1), lit("key2"), lit(2)))
To provide names for structs use any alias in each field:
df.withColumn( "some_struct", struct(lit("foo").alias("x"), lit(1).alias("y"), lit(0.3).alias("z")) )
or cast on the whole object
df.withColumn( "some_struct", struct(lit("foo"), lit(1), lit(0.3)).cast("struct<x: string, y: integer, z: double>") )
It is also possible, albeit slower, to use UDF.
Note :
The same constructs can be used to pass constant arguments to UDF or SQL functions.