nerdexam
Databricks

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

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

The correct answer is C. storesDF.groupBy("division", "storeCategory").count(). The correct syntax for grouping by multiple columns using string names is storesDF.groupBy("division", "storeCategory").count(). Spark's groupBy() accepts column names as a varargs list of strings. Option A and E pass a Seq, which is not the correct argument type for groupBy()…

Performing Data Aggregations with Spark DataFrames

Question

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

Options

  • AstoresDF.groupBy(Seq(col("division"), col("storeCategory"))).count()
  • BstoresDF.groupBy(division, storeCategory).count()
  • CstoresDF.groupBy("division", "storeCategory").count()
  • DstoresDF.groupBy("division").groupBy("StoreCategory").count()
  • EstoresDF.groupBy(Seq("division", "storeCategory")).count()

How the community answered

(15 responses)
  • B
    7% (1)
  • C
    87% (13)
  • D
    7% (1)

Explanation

The correct syntax for grouping by multiple columns using string names is storesDF.groupBy("division", "storeCategory").count(). Spark's groupBy() accepts column names as a varargs list of strings. Option A and E pass a Seq, which is not the correct argument type for groupBy() without the _* spread operator. Option B passes bare identifiers (not valid as column references). Option D chains two separate groupBy() calls, which means the second call replaces the first - effectively grouping only by storeCategory.

Topics

#Spark DataFrame#Grouping#Aggregation

Community Discussion

No community discussion yet for this question.

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