2

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

2
  • 2
    df.withColumn('DATE', lit(f'{date}'))? Commented Aug 28, 2021 at 11:29
  • df = df.withColumn("DATE", lit(str(date))) Commented Aug 28, 2021 at 13:43

1 Answer 1

3

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

Example:

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

#or

df.withColumn('DATE', lit(f'{date}'))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the first two worked for me, the third one did not ... maybe it has to do something with the python version or functions? Anyway, solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.