Revenue by state

joins

Revenue by state

Amazon Pandas Interview Question

Amazon's regional marketing team wants to know which states its delivered revenue comes from.

Using the orders and customers DataFrames, find each customer state and the total revenue from delivered orders as revenue, rounded to 2 decimal places. Revenue is the quantity sold times the unit price. Sort the rows by revenue, highest first. Assign the answer to result.

Asked of

  • Data Analyst
  • Product Analyst
  • Business Analyst
  • Analytics Engineer
  • Data Scientist

ordersDataFrame30 rows

Column NameType
order_idint64
customer_idint64
order_datestr
categorystr
quantityint64
unit_pricefloat64
statusstr

customersDataFrame15 rows

Column NameType
customer_idint64
customer_namestr
statestr
signup_datestr
prime_memberbool

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

customersExample Input

customer_idcustomer_namestatesignup_dateprime_member
501Avery JohnsonCA2022-03-14true
502Mateo RiveraTX2021-11-02false
503Hannah BrooksNY2023-01-19true
504Jamal CarterWA2022-08-07true
505Sofia NguyenCA2020-05-25true
506Ethan WalshTX2023-06-30false
507Leila HaddadNY2021-02-11true
508Ryan PatelFL2022-12-03false

Example Output

staterevenue
TX400
CA64.97
NY35.88
WA25.5

Explanation

In the example, customers in Texas had delivered orders worth 249.00, 22.00 and 129.00, a total of 400.00, so Texas comes first. Returned and canceled orders are left out, which is why New York only counts one order, worth 35.88.

The example above is a small slice of the data. Your code runs against the full DataFrames.