Revenue by category

aggregations

Revenue 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 NameType
order_idint64
customer_idint64
order_datestr
categorystr
quantityint64
unit_pricefloat64
statusstr

ordersExample Input

order_idcustomer_idorder_datecategoryquantityunit_pricestatus
100015012024-05-01Books214.99delivered
100025022024-05-01Electronics1249delivered
100035032024-05-02Home319.5returned
100045012024-05-02Toys134.99delivered
100055042024-05-03Grocery64.25delivered
100065052024-05-03Electronics289.99canceled
100075022024-05-04Books122delivered
100085062024-05-04Home1129delivered
100095072024-05-05Grocery122.99delivered
100105032024-05-05Electronics159.99returned

Example Output

categoryrevenue
Books51.98
Electronics249
Grocery61.38
Home129
Toys34.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.