Pandas Outer Merge With Zero Fill for Two Store Inventories
Quick Overview
Write a pandas function that combines inventory counts from two stores into one table holding every SKU from either store, with missing counts set to zero. It tests full outer merges, how missing values change column dtypes, and returning an exact column order, sort and index.
Pandas Outer Merge With Zero Fill for Two Store Inventories
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Write `outer_fill(store_a, store_b)` that combines inventory counts from two stores. `store_a` has `sku` (str) and `qty_a` (int). `store_b` has `sku` (str) and `qty_b` (int). Return a DataFrame with every SKU present in either frame.
Requirements:
- Full outer merge on `sku`.
- Fill missing `qty_a` / `qty_b` with 0, then cast both to int.
- Result columns exactly `['sku', 'qty_a', 'qty_b']`.
- Sort by `sku` ascending and reset to a fresh `RangeIndex` (drop the old index).
Example:
```python
store_a = pd.DataFrame({'sku': ['x', 'y'], 'qty_a': [3, 5]})
store_b = pd.DataFrame({'sku': ['y', 'z'], 'qty_b': [2, 7]})
outer_fill(store_a, store_b)
# sku qty_a qty_b
# 0 x 3 0
# 1 y 5 2
# 2 z 0 7
```
```hint Check the dtypes
Print `dtypes` on the merged frame before you fill anything and look at what happened to the two integer columns.
```
### What a Strong Answer Covers
- A full outer merge on `sku`, so keys found in only one frame survive.
- Why the quantity columns change dtype after the merge, and the order of the fill and the cast.
- The exact output shape: three columns in the required order, rows sorted by `sku`, and a fresh `RangeIndex` with the old index dropped.
- Edge cases such as a SKU repeated within one frame or a quantity that is already missing in the input.
### Follow-up Questions
- How would you also flag which store or stores each SKU came from?
- Why does the cast to int have to come after `fillna`?
- What happens if a SKU appears twice in `store_a`, and how would you catch or handle it?
Overview: Write a pandas function that combines inventory counts from two stores into one table holding every SKU from either store, with missing counts set to zero. It tests full outer merges, how missing values change column dtypes, and returning an exact column order, sort and index.
Write outer_fill(store_a, store_b) that combines inventory counts from two stores. store_a has sku (str) and qty_a (int). store_b has sku (str) and qty_b (int). Return a DataFrame with every SKU present in either frame.
Requirements:
Full outer merge on
sku
.
Fill missing
qty_a
/
qty_b
with 0, then cast both to int.
Result columns exactly
['sku', 'qty_a', 'qty_b']
.
Sort by
sku
ascending and reset to a fresh
RangeIndex
(drop the old index).