AnalystPath

Count Bottles and Cans Across Pallets

PandasMediumMid level~10 min

Problem

You are given two DataFrames. `pallet` has columns `pallet_id`, `crate_id`, `bottle_qty`, `can_qty`; its `crate_id` may be missing when a pallet carries no crate. `crate` has columns `crate_id`, `bottle_qty`, `can_qty`.

For every pallet, its contribution is its own quantities plus the quantities of the crate it references (zero when it has no crate). Return the grand total bottles and cans across all pallets as a single row. Output columns: `bottle_qty`, `can_qty`.

Input data

Example rows — the live problem includes the full dataset.

pallet
pallet_idcrate_idbottle_qtycan_qty
110052
234
310101
crate
crate_idbottle_qtycan_qty
1001020
10170

Expected output

Your answer should return 1 row with the columns bottle_qty, can_qty.

Starter code (Pandas (Python))

import pandas as pd

def count_bottles_and_cans(pallet, crate) -> pd.DataFrame:
    # Your code here
    return pallet

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