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