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