1. Group rows
groupBy buckets rows by the given column so each country becomes one group.
df.groupBy("country")Typorithm · medium
Step 1 of 8 · intro card
Transformations & Actions
groupBy buckets rows by a column; agg applies accumulators like sum and avg to each group, collapsing it to one row. orderBy(desc(...)) sorts the aggregated output.
groupBy partitions the DataFrame into groups keyed by one or more columns — here every distinct country becomes a group.
agg applies aggregate functions to each group: sum("revenue").alias("total") accumulates revenue and names the column, while avg("revenue") computes the per-group mean.
Each group collapses to a single row, and orderBy(desc("total")) sorts those rows so the largest total comes first; show() then prints them.
groupBy buckets rows by a column.
agg runs accumulators like sum and avg per group.
orderBy(desc(...)) sorts the aggregated rows.
df.groupBy("country").agg(sum("revenue").alias("total"), avg("revenue")).orderBy(desc("total")).show();Connect each part of the code to the concept before you type.
groupBy buckets rows by the given column so each country becomes one group.
df.groupBy("country")agg runs accumulators like sum and avg over each group, collapsing it to one row.
agg(sum("revenue").alias("total"), avg("revenue"))alias renames the aggregated column so you can reference it later, e.g. when ordering.
.alias("total")orderBy(desc(...)) sorts the grouped result so the highest total appears first.
orderBy(desc("total"))Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.