nerdexam
Databricks

DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #156

Which of the following code blocks fails to return the number of rows in DataFrame storesDF for each distinct combination of values in column division and column storeCategory?

The correct answer is B. storesDF.groupBy("division").groupBy("storeCategory").count(). Chaining two separate groupBy() calls does not combine both columns into a single grouping. The second groupBy() call completely replaces the first, so the result groups only by storeCategory - division is silently ignored. The correct approaches all pass both columns in a…

Spark SQL and DataFrames

Question

Which of the following code blocks fails to return the number of rows in DataFrame storesDF for each distinct combination of values in column division and column storeCategory?

Options

  • AstoresDF.groupBy((col("division"), col("storeCategory")]).count()
  • BstoresDF.groupBy("division").groupBy("storeCategory").count()
  • CstoresDF.groupBy(["division", "storeCategory"]).count()
  • DstoresDF.groupBy("division", "storeCategory").count()
  • EstoresDF.groupBy(col("division?, col("storeCategory")).count()

How the community answered

(34 responses)
  • A
    3% (1)
  • B
    88% (30)
  • C
    6% (2)
  • D
    3% (1)

Explanation

Chaining two separate groupBy() calls does not combine both columns into a single grouping. The second groupBy() call completely replaces the first, so the result groups only by storeCategory - division is silently ignored. The correct approaches all pass both columns in a single groupBy() call: as multiple string arguments (.groupBy('division', 'storeCategory')), as a list (.groupBy(['division', 'storeCategory'])), or as Column objects (.groupBy(col('division'), col('storeCategory'))). Options A, C, D, and E all achieve the intended two-column grouping.

Topics

#PySpark DataFrames#groupBy#Aggregations#DataFrame Transformations

Community Discussion

No community discussion yet for this question.

Full DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK Practice