What does the groupBy() function do in Apache Spark?
It groups rows in a DataFrame based on one or more columns, so you can perform aggregations on each group separately.
Click to reveal answer
beginner
Name three common aggregation functions used after groupBy() in Spark.
Common aggregation functions are count(), sum(), and avg() (average).
Click to reveal answer
beginner
How do you calculate the average of a column named sales after grouping by region?
Use df.groupBy('region').avg('sales') to get the average sales per region.
Click to reveal answer
intermediate
What is the difference between agg() and direct aggregation functions like sum() after groupBy()?
agg() lets you apply multiple aggregation functions at once, while direct functions like sum() apply a single aggregation.
Click to reveal answer
beginner
Why is grouping and aggregation useful in real life?
It helps summarize large data sets, like finding total sales per store or average temperature per city, making data easier to understand.
Click to reveal answer
What does df.groupBy('category').count() do?
ACounts the number of columns in the DataFrame
BCounts the total number of categories
CCounts the number of unique values in the DataFrame
DCounts the number of rows in each category group
✗ Incorrect
The count() after groupBy() counts rows per group.
Which function calculates the sum of a column after grouping?
Asum()
Bavg()
Ccount()
Dmax()
✗ Incorrect
sum() adds up values in each group.
How do you apply multiple aggregations like sum and average together?
AUse multiple groupBy() calls
BUse agg() with a dictionary of functions
CUse count() twice
DUse select() only
✗ Incorrect
The agg() function allows multiple aggregations at once.
What type of object does groupBy() return?
AGroupedData
BDataFrame
CRDD
DList
✗ Incorrect
groupBy() returns a GroupedData object to apply aggregations.
Which of these is NOT an aggregation function in Spark?
Amax()
Bmin()
Cfilter()
Davg()
✗ Incorrect
filter() is for filtering rows, not aggregation.
Explain how you would use groupBy() and aggregation functions to find the total sales per product category.
Think about grouping by category and then summing sales.
You got /3 concepts.
Describe the difference between using agg() and direct aggregation functions after groupBy().
Consider when you want one vs many summary stats.
You got /3 concepts.
Practice
(1/5)
1. What does the groupBy function do in Apache Spark?
easy
A. It splits the data into groups based on column values.
B. It sorts the data in ascending order.
C. It filters rows based on a condition.
D. It joins two DataFrames together.
Solution
Step 1: Understand the purpose of groupBy
The groupBy function is used to split data into groups based on one or more column values.
Step 2: Compare with other functions
Sorting, filtering, and joining are different operations and do not split data into groups.
Final Answer:
It splits the data into groups based on column values. -> Option A
Quick Check:
groupBy = splits data into groups [OK]
Hint: Remember: groupBy groups rows by column values [OK]
Common Mistakes:
Confusing groupBy with filter or sort
Thinking groupBy changes row order
Assuming groupBy merges DataFrames
2. Which of the following is the correct syntax to calculate the average of column sales after grouping by region in Spark DataFrame df?
easy
A. df.groupBy("region").sum("sales")
B. df.groupBy("region").agg(avg("sales"))
C. df.groupBy("region").filter(avg("sales"))
D. df.groupBy("region").select(avg("sales"))
Solution
Step 1: Identify correct aggregation syntax
To calculate average after grouping, use groupBy followed by agg(avg(column)).
Step 2: Check each option
df.groupBy("region").agg(avg("sales")) uses agg(avg("sales")) correctly. df.groupBy("region").sum("sales") uses sum, not average. df.groupBy("region").filter(avg("sales")) uses filter incorrectly. df.groupBy("region").select(avg("sales")) uses select which is not valid after groupBy for aggregation.
Final Answer:
df.groupBy("region").agg(avg("sales")) -> Option B
A. Missing alias for aggregated columns causes error.
B. agg() requires a dictionary, not multiple arguments.
C. groupBy cannot be used with agg.
D. sum and avg functions are not imported.
Solution
Step 1: Check function imports
The code uses sum and avg functions but does not show imports from pyspark.sql.functions.
Step 2: Validate other options
agg() accepts multiple arguments; aliasing is optional; groupBy works with agg.
Final Answer:
sum and avg functions are not imported. -> Option D
Quick Check:
Missing imports cause NameError [OK]
Hint: Always import aggregation functions before use [OK]
Common Mistakes:
Assuming agg needs dict, but it accepts multiple args
Forgetting to import sum, avg from pyspark.sql.functions
Thinking groupBy can't be chained with agg
5. You have a Spark DataFrame sales_df with columns store, product, and revenue. You want to find the maximum revenue per store and the average revenue per product across all stores in a single query. Which code snippet correctly achieves this?
hard
A. sales_df.groupBy("store").agg(max("revenue").alias("max_revenue"), avg("revenue").alias("avg_revenue"))
B. sales_df.agg(max("revenue").alias("max_revenue"), avg("revenue").alias("avg_revenue"))
C. sales_df.groupBy("store").agg(max("revenue").alias("max_revenue")).crossJoin(sales_df.groupBy("product").agg(avg("revenue").alias("avg_revenue")))
D. sales_df.groupBy("store").agg(max("revenue").alias("max_revenue")).groupBy("product").agg(avg("revenue").alias("avg_revenue"))
Solution
Step 1: Understand the requirements
We need max revenue per store and average revenue per product in one result.
Step 2: Analyze options
sales_df.groupBy("store").agg(max("revenue").alias("max_revenue"), avg("revenue").alias("avg_revenue")) aggregates both by store only, so avg revenue per product is incorrect. sales_df.agg(max("revenue").alias("max_revenue"), avg("revenue").alias("avg_revenue")) aggregates globally, losing grouping. sales_df.groupBy("store").agg(max("revenue").alias("max_revenue")).groupBy("product").agg(avg("revenue").alias("avg_revenue")) tries chaining groupBy but overwrites previous aggregation. sales_df.groupBy("store").agg(max("revenue").alias("max_revenue")).crossJoin(sales_df.groupBy("product").agg(avg("revenue").alias("avg_revenue"))) computes both groupings separately and joins results, correctly combining both summaries.
Final Answer:
sales_df.groupBy("store").agg(max("revenue").alias("max_revenue")).crossJoin(sales_df.groupBy("product").agg(avg("revenue").alias("avg_revenue"))) -> Option C
Quick Check:
Separate groupBy + crossJoin combines both aggregations [OK]
Hint: Use separate groupBy then join to combine different aggregations [OK]
Common Mistakes:
Trying to aggregate different groupings in one groupBy
Overwriting aggregation by chaining groupBy
Forgetting to join separate aggregation results
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.