
Revenue by category
easyRevenue by category
Amazon Pandas Interview Question
Amazon's category managers want to know which product categories bring in the most money from orders that actually reached customers.
Using the orders DataFrame, find the total revenue from delivered orders for each category, as revenue, rounded to 2 decimal places. Revenue is the quantity sold times the unit price. Assign the answer to result.
Asked of
- Data Analyst
- Product Analyst
- Analytics Engineer
- Data Scientist
- Data Engineer
ordersDataFrame30 rows
| Column Name | Type |
|---|---|
| order_id | int64 |
| customer_id | int64 |
| order_date | str |
| category | str |
| quantity | int64 |
| unit_price | float64 |
| status | str |
ordersExample Input
| order_id | customer_id | order_date | category | quantity | unit_price | status |
|---|---|---|---|---|---|---|
| 10001 | 501 | 2024-05-01 | Books | 2 | 14.99 | delivered |
| 10002 | 502 | 2024-05-01 | Electronics | 1 | 249 | delivered |
| 10003 | 503 | 2024-05-02 | Home | 3 | 19.5 | returned |
| 10004 | 501 | 2024-05-02 | Toys | 1 | 34.99 | delivered |
| 10005 | 504 | 2024-05-03 | Grocery | 6 | 4.25 | delivered |
| 10006 | 505 | 2024-05-03 | Electronics | 2 | 89.99 | canceled |
| 10007 | 502 | 2024-05-04 | Books | 1 | 22 | delivered |
| 10008 | 506 | 2024-05-04 | Home | 1 | 129 | delivered |
| 10009 | 507 | 2024-05-05 | Grocery | 12 | 2.99 | delivered |
| 10010 | 503 | 2024-05-05 | Electronics | 1 | 59.99 | returned |
Example Output
| category | revenue |
|---|---|
| Books | 51.98 |
| Electronics | 249 |
| Grocery | 61.38 |
| Home | 129 |
| Toys | 34.99 |
Explanation
Books had two delivered orders: 2 items at 14.99 and 1 item at 22.00, which adds up to 51.98. Home had one returned order and one delivered order for 129.00, and only the delivered order counts. Electronics earned 249.00, because its other two orders were canceled or returned.
The example above is a small slice of the data. Your code runs against the full DataFrames.
Company
Amazon
Difficulty
easy
Topic
aggregations
Language
Pandas
Your code
Loading Python in the background. You can start writing.