Items and aisles per order

aggregations

Items and aisles per order

Instacart Pandas Interview Question

Instacart shoppers plan their route through a store around how many items an order has and how many aisles they need to visit.

Using the items DataFrame, find each order_id, the total number of items in that order as total_items, and the number of different aisles it covers as aisles. Assign the answer to result.

Asked of

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

itemsDataFrame22 rows

Column NameType
order_idint64
product_namestr
aislestr
quantityint64
unit_pricefloat64

itemsExample Input

order_idproduct_nameaislequantityunit_price
5001BananasProduce60.25
5001Oat MilkDairy24.49
5001SpinachProduce13.99
5002Sourdough BreadBakery15.5
5002EggsDairy13.29
5003ApplesProduce80.6
5003Greek YogurtDairy41.25
5003BagelsBakery60.9
5003Cheddar CheeseDairy16.75

Example Output

order_idtotal_itemsaisles
500192
500222
5003193

Explanation

Order 5001 has 6 bananas, 2 oat milks and 1 bag of spinach, which is 9 items from 2 aisles, Produce and Dairy. Order 5003 has 19 items from 3 aisles, because the yogurt and the cheese both come from Dairy and that aisle only counts once.

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