GroupBy with Multiple Aggregations

Pandas data engineering interview problem. Difficulty: beginner. Pattern: GroupBy. About 12 minutes. Free to practice.

Per-region totals, average, count, and max amount in pandas. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A sales manager wants a short summary for each region. From the DataFrame sales, group the rows by region and calculate four numbers for each region: total_amount (the sum of the amounts), avg_amount (the average), txn_count (how many sales rows) and max_amount (the biggest single amount). Return 1 row per region, with the columns region, total_amount, avg_amount, txn_count and max_amount, sorted by region. Save the final table in result.

Requirements

  • Return 1 row for each region.
  • Calculate all 4 metrics: sum, mean, count and max of amount.
  • Name the columns exactly total_amount, avg_amount, txn_count and max_amount.
  • Make region a normal column, not the index.
  • Sort by region.

Constraints

  • sales has the columns date, product, region and amount.
  • Use groupby('region').agg(...) with named aggregation, for example total_amount=('amount', 'sum').
  • Call reset_index() after grouping so region is a column again.

Examples

Input: sales Output: region | total_amount | avg_amount | txn_count | max_amount EU | 70.0 | 35.0 | 2 | 40.0 US | 140.0 | 35.0 | 4 | 60.0 Named aggregations keep column contracts clear.

Topics: lakebench, pandas, groupby, agg.

More interview problems · All interview problems · Learn data engineering

beginner

GroupBy with Multiple Aggregations

Interview-style drill: Per-region totals, average, count, and max amount in pandas.

From `sales`, group by `region` and compute `total_amount`, `avg_amount`, `txn_count`, `max_amount`. Sort by `region`. Assign `result`.