
Answer-first summary for fast verification
Answer: df.withColumn('day_of_week', dayofweek('timestamp'))
The correct answer is A because it correctly uses the `dayofweek` function to extract the day of the week from the `timestamp` column and adds it as a new column `day_of_week`.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
You have a DataFrame df with a column timestamp in timestamp format. How would you extract the day of the week from this timestamp and create a new column day_of_week using Spark? Provide the code snippet.
A
df.withColumn('day_of_week', dayofweek('timestamp'))
B
df.select(dayofweek('timestamp'))
C
df.withColumn('day_of_week', date_format('timestamp', 'E'))
D
df.select(date_format('timestamp', 'E'))