Ingredients Frequently Used Together
Problem
You are given two DataFrames. `recipe_uses` (loaded from `recipe_uses.csv`) has columns `cook_id`, `item_id`, and `grams`; each row means a cook used a given pantry item in one of their recipes. `pantry_items` (loaded from `pantry_items.csv`) has columns `item_id`, `aisle`, and `unit_cost`, with `item_id` as its primary key.
Find every pair of pantry items that were used together by at least three different cooks. Treat each pair only once, with the smaller `item_id` first. Return the two item ids, the aisle of each item, and the number of distinct cooks who used both.
Return `item1_id`, `item2_id`, `item1_aisle`, `item2_aisle`, and `cook_count`, ordered by `cook_count` descending, then `item1_id` ascending, then `item2_id` ascending.
Input data
Example rows — the live problem includes the full dataset.
| cook_id | item_id | grams |
|---|---|---|
| 1 | 101 | 200 |
| 1 | 102 | 100 |
| 1 | 103 | 300 |
| 2 | 101 | 100 |
| 2 | 102 | 500 |
| item_id | aisle | unit_cost |
|---|---|---|
| 101 | Produce | 1.00 |
| 102 | Baking | 0.20 |
| 103 | Dairy | 0.35 |
| 104 | Spices | 0.50 |
| 105 | Grains | 0.75 |
Expected output
Your answer should return 3 rows with the columns item1_id, item2_id, item1_aisle, item2_aisle, cook_count.
Starter code (Pandas (Python))
import pandas as pd
def frequent_ingredient_pairs(recipe_uses, pantry_items) -> pd.DataFrame:
# Your code here
return recipe_usesSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock