AnalystPath

Was the Second Supply a Preferred Vendor?

PandasHardSenior level~10 min

Problem

You are given three DataFrames. `contractors` (from `contractors.csv`) has `contractor_id` (the key), `onboarded_day` and `preferred_vendor`. `supplies` (from `supplies.csv`) has `supply_id` (the key), `supplied_day`, `material_id` and `contractor_id` — the contractor who supplied the material (assume each contractor's supply dates are distinct). `materials` (from `materials.csv`) has `material_id` (the key) and `vendor`.

For each contractor, determine whether the **vendor of the second material they supplied (ordered by supply date)** equals their `preferred_vendor`. If the contractor supplied fewer than two materials, report `'no'`. Return `contractor_id` and a column named `second_supply_preferred` holding `'yes'` or `'no'`.

Example: contractor 1 prefers Acme; their second supply (material 200) is from Acme -> yes. Contractor 2 prefers Globex but supplied only one material -> no.

Input data

Example rows — the live problem includes the full dataset.

contractors
contractor_idonboarded_daypreferred_vendor
12023-01-01Acme
22023-01-01Globex
supplies
supply_idsupplied_daymaterial_idcontractor_id
102023-02-011001
112023-02-052001
122023-03-013002
materials
material_idvendor
100Globex
200Acme
300Globex

Expected output

Your answer should return 2 rows with the columns contractor_id, second_supply_preferred.

Starter code (Pandas (Python))

import pandas as pd

def second_supply_check(contractors, supplies, materials) -> pd.DataFrame:
    # Your code here
    return contractors

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