Python
All Top Python Interview Questions#31
Multiple Named Aggregations with groupby().agg() in Pandas
EasyAmazonInterview Question #31
Asked at AmazonGiven an `orders_df` (department, order_id, customer_id, revenue), write a query to compute: total revenue (`total_rev`), average revenue (`avg_rev`), total orders (`order_count`), and unique customers (`unique_buyers`) per department.
Input Table: df
3 rows preview| department | order_id | customer_id | revenue |
|---|---|---|---|
| IT | 101 | 1 | 5000 |
| IT | 102 | 2 | 7000 |
| Sales | 103 | 1 | 3000 |
Expected Output Structure2 rows
| department | total_rev | avg_rev | order_count | unique_buyers |
|---|---|---|---|---|
| IT | 12000 | 6000 | 2 | 2 |
| Sales | 3000 | 3000 | 1 | 1 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Starting the real Python + pandas runtime (one-time download)…