QuestionQ122

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

The code block below has an error. It is intended to return a new DataFrame in which the productCategories column contains only one word per row, producing a DataFrame with many more rows than storesDF. Identify the error and how to correct it.

A sample of storesDF is shown below:

Question Image

storesDF.withColumn("productCategories", split(col("productCategories")))  
Explanation

Spark explode creates a separate row for every element of an array or map. Applying it to productCategories yields one category per row and increases the row count; split instead converts a string into an array and does not perform that row expansion.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!