DATABRICKS-CERTIFIED-ASSOCIATE-DEVELOPER-FOR-APACHE-SPARK · Question #58
Which of the following code blocks returns the number of rows in DataFrame storesDF for each unique value in column division?
The correct answer is E. storesDF.groupBy("division").count(). To count rows per group in PySpark, use groupBy(column).count(). Option E - storesDF.groupBy('division').count() - correctly groups by 'division' and counts the rows in each group. Option A calls count() without any argument inside agg(), which requires count('*') or…
Question
Which of the following code blocks returns the number of rows in DataFrame storesDF for each unique value in column division?
Options
- AstoresDF.groupBy("division").agg(count())
- BstoresDF.agg(groupBy("division").count())
- CstoresDF.groupby.count("division")
- DstoresDF.groupBy().count("division")
- EstoresDF.groupBy("division").count()
How the community answered
(28 responses)- A4% (1)
- C4% (1)
- D4% (1)
- E89% (25)
Explanation
To count rows per group in PySpark, use groupBy(column).count(). Option E - storesDF.groupBy('division').count() - correctly groups by 'division' and counts the rows in each group. Option A calls count() without any argument inside agg(), which requires count('*') or count(col(...)) to work. Option B reverses the order - agg() and groupBy() cannot be chained that way. Option C uses lowercase groupby with incorrect .count('division') syntax. Option D calls groupBy() with no arguments, meaning no grouping column is specified, and then .count('division') is not valid syntax for the count method.
Topics
Community Discussion
No community discussion yet for this question.