Adding a nullable column in Spark dataframe
Asked Answered
P

1

6

In Spark, literal columns, when added, are not nullable:

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

df = spark.createDataFrame([(1,)], ['c1'])

df = df.withColumn('c2', F.lit('a'))

df.printSchema()
#  root
#   |-- c1: long (nullable = true)
#   |-- c2: string (nullable = false)

How to create a nullable column?

Putdown answered 29/7, 2021 at 14:56 Comment(5)
the real question is "why would you need to have a lit null column ...Correction
In my case, I needed to create the schema identic to another dataframe, but with different data in it. This includes nullability.Putdown
why don't you create a schema directly then ? starting from df.schema.Correction
I thnik you should create another question with your original usecase. Currently, you are trying to find help for a solution you imagined but probably not the proper one. It is called XY_problemCorrection
Sorry, but If you cannot imagine a use case it does not mean it does not exist. I had this issue and found an answer here. Then I made it even better so I decided to post it here. Later I read more and found this highly-upvoted answer to a different problem. I think those cases prove that use case exists, whatever original issue people may have. I just wanted to help someone else to find the answer easier.Putdown
P
10

The shortest method I've found - using when (the otherwise clause seems not needed):

df = df.withColumn('c2', F.when(F.lit(True), F.lit('a')))

If in Scala: .withColumn("c2", when(lit(true), lit("a")))


Full test result:

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()

df = spark.createDataFrame([(1,)], ['c1'])
df = df.withColumn('c2', F.when(F.lit(True), F.lit('a')))

df.show()
#  +---+---+
#  | c1| c2|
#  +---+---+
#  |  1|  a|
#  +---+---+

df.printSchema()
#  root
#   |-- c1: long (nullable = true)
#   |-- c2: string (nullable = true)
Putdown answered 29/7, 2021 at 14:56 Comment(1)
Uau, pretty good man, you saved me! I wouldn't like to build so much code to do something tiny. Thank you!Simons

© 2022 - 2024 — McMap. All rights reserved.