AnalystPath

Ingredients Frequently Used Together

PandasMediumMid level~10 min

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.

recipe_uses
cook_iditem_idgrams
1101200
1102100
1103300
2101100
2102500
pantry_items
item_idaisleunit_cost
101Produce1.00
102Baking0.20
103Dairy0.35
104Spices0.50
105Grains0.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_uses

Solve 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

Related Pandas questions