Pyspark: Pass parameter to String Column in Dataframe

I’m quite new to PySpark and coming from SAS I still don’t get how to handle parameters (or Macro Variables in SAS terminology).

I have a date parameter like “202105” and want to add it as a String Column to a Dataframe. Something like this:

date = 202105
df = df.withColumn("DATE", lit('{date}'))

I think it’s quite trivial but so far, I didn’t find an exact answer to my problem, maybe it’s just too trivial…

Hope you guys can help me out. Best regards


You can use string interpolations i.e. {}.format() (or) f'{}'.


df.withColumn("DATE", lit("{0}".format(date)))
df.withColumn("DATE", lit("{}".format(date)))


df.withColumn('DATE', lit(f'{date}'))