
Answer-first summary for fast verification
Answer: df.withColumn('city', col('address.city'))
The correct answer is A because it correctly uses the dot syntax to access the nested `city` field within the `address` column and adds it as a new top-level column named `city`.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You have a DataFrame df with a nested column address.city. How would you extract this nested field into a top-level column named city using Spark? Provide the code snippet.
A
df.withColumn('city', col('address.city'))
B
df.select(col('address.city').alias('city'))
C
df.withColumn('city', 'address.city')
D
df.select('address.city').alias('city')
No comments yet.