Build a pandas DataFrame on a Cartesian product MultiIndex
Quick Overview
Write a pandas function that builds a MultiIndex from the Cartesian product of two lists and returns a one-column DataFrame filled with a constant. It tests MultiIndex construction, row ordering, scalar broadcasting in the DataFrame constructor, and control over the column dtype.
Build a pandas DataFrame on a Cartesian product MultiIndex
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `grid_frame(levels: list, names: list, fill: int) -> pd.DataFrame`. Build the index with `pd.MultiIndex.from_product(levels, names=names)` from the two level lists, and return a single-column DataFrame `{'value': fill}` broadcast over every row.
The result must meet these requirements:
- The index is the Cartesian product of the two lists, with its levels named by `names`.
- Rows follow `from_product` order: the first level varies slowest.
- There is exactly one column, `'value'`, filled with `fill`, with dtype `int64`.
- The output must match the expected frame under `assert_frame_equal`.
Example:
```python
grid_frame([['a', 'b'], [1, 2]], ['letter', 'num'], 0)
# value
# letter num
# a 1 0
# 2 0
# b 1 0
# 2 0
```
```hint Scalar broadcasting
Look at what the DataFrame constructor does with a scalar column value, and at what it needs to know before it can repeat that value. Then check which dtype a plain Python int produces.
```
### What a Strong Answer Covers
- How the index is built so that both the row order and the level names match the requirements.
- How the single scalar `fill` ends up on every row, and what the constructor needs for that to work.
- Which dtype the `value` column gets and why.
- Edge cases such as an empty level, a `names` list of the wrong length, and a `fill` that is not a plain int.
### Follow-up Questions
- How would you generalize to N levels?
- How do you guarantee the `value` column is `int64` even when callers pass something other than a Python int?
Overview: Write a pandas function that builds a MultiIndex from the Cartesian product of two lists and returns a one-column DataFrame filled with a constant. It tests MultiIndex construction, row ordering, scalar broadcasting in the DataFrame constructor, and control over the column dtype.
Implement grid_frame(levels: list, names: list, fill: int) -> pd.DataFrame. Build the index with pd.MultiIndex.from_product(levels, names=names) from the two level lists, and return a single-column DataFrame {'value': fill} broadcast over every row.
The result must meet these requirements:
The index is the Cartesian product of the two lists, with its levels named by
names
.
Rows follow
from_product
order: the first level varies slowest.
There is exactly one column,
'value'
, filled with
fill
, with dtype
int64
.
The output must match the expected frame under
assert_frame_equal
.
Example:
grid_frame([['a', 'b'], [1, 2]], ['letter', 'num'], 0)
# value
# letter num
# a 1 0
# 2 0
# b 1 0
# 2 0
What a Strong Answer Covers Guidance
How the index is built so that both the row order and the level names match the requirements.
How the single scalar
fill
ends up on every row, and what the constructor needs for that to work.
Which dtype the
value
column gets and why.
Edge cases such as an empty level, a
names
list of the wrong length, and a
fill
that is not a plain int.
Follow-up Questions Guidance
How would you generalize to N levels?
How do you guarantee the
value
column is
int64
even when callers pass something other than a Python int?