
Answer-first summary for fast verification
Answer: storesDF.withColumn("productCategories", explode(col("productCategories"))), storesDF.withColumn("productCategories", explode("productCategories"))
The correct answer uses the `explode` function, which transforms each element of an array (or list) in a column into a separate row. This results in a DataFrame with more rows, each containing a single element from the original array. Options A and E are valid because `explode` can accept either a `Column` object (using `col("productCategories")`) or a column name string ("productCategories"). Options B and D incorrectly use `split`, which is for converting strings to arrays, not exploding existing arrays. Option C uses invalid syntax (`col().explode()`) since `explode` is a function, not a column method.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
Which of the following code blocks returns a new DataFrame where the column productCategories contains only one word per row, resulting in a DataFrame with significantly more rows than the original storesDF?
A sample of storesDF is shown below:
storeId productCategories
0 [netus, pellentes...]
1 [consequat enim,...]
2 [massa, a, vitae,...]
3 [aliquam, donec...]
4 [condimentum, fer...]
5 [viverra habitan...]
storeId productCategories
0 [netus, pellentes...]
1 [consequat enim,...]
2 [massa, a, vitae,...]
3 [aliquam, donec...]
4 [condimentum, fer...]
5 [viverra habitan...]
A
storesDF.withColumn("productCategories", explode(col("productCategories")))
B
storesDF.withColumn("productCategories", split(col("productCategories")))
C
storesDF.withColumn("productCategories", col("productCategories").explode())
D
storesDF.withColumn("productCategories", col("productCategories").split())
E
storesDF.withColumn("productCategories", explode("productCategories"))
No comments yet.