Build a pandas DataFrame with a two-level MultiIndex using from_arrays

Quick Overview

Build a pandas DataFrame with a two-level MultiIndex from parallel label lists using MultiIndex.from_arrays, keeping the input row order. It tests hierarchical index construction, positional alignment of values, level names and what assert_frame_equal checks.

Build a pandas DataFrame with a two-level MultiIndex using from_arrays

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `build_multiindex_frame(arrays, names, values)`. `arrays` is a list of two equal-length label lists (level 0, level 1), `names` gives the two level names, and `values` is a list of ints. Return a single-column DataFrame (column `'value'`) indexed by a two-level `MultiIndex`. The function must meet these requirements: - Build the index with `pd.MultiIndex.from_arrays`. - Do not sort the index. The rows keep the input order. - The column name, dtype, level names and row order must match the expected frame exactly, since the result is compared with `assert_frame_equal`. - All three lists have consistent lengths, so no validation is required. For example: ```python arrays = [['a', 'a', 'b'], [1, 2, 1]] names = ['letter', 'num'] values = [10, 20, 30] build_multiindex_frame(arrays, names, values) # value # letter num # a 1 10 # 2 20 # b 1 30 ``` ```hint Pair by position Element i of each label list belongs to row i. Check that the constructor you pick pairs the lists that way, and that the integers line up with the rows by position too. ``` ### What a Strong Answer Covers - How `from_arrays` pairs the i-th label of each list into one index entry and attaches the level names - How the integer list lands in the `value` column aligned with the index by position - Why the row order matches the input, and which operations would reorder it - The dtype of the value column and of each level, and what `assert_frame_equal` compares ### Follow-up Questions - How would this differ using `from_tuples` or `from_product`? - Why might `assert_frame_equal` fail even when the values look identical? - If `values` arrived as a pandas Series with its own index, what would the result look like, and how would you fix it? - What changes when you select or slice rows on a MultiIndex that is not sorted?

Overview: Build a pandas DataFrame with a two-level MultiIndex from parallel label lists using MultiIndex.from_arrays, keeping the input row order. It tests hierarchical index construction, positional alignment of values, level names and what assert_frame_equal checks.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement build_multiindex_frame(arrays, names, values). arrays is a list of two equal-length label lists (level 0, level 1), names gives the two level names, and values is a list of ints. Return a single-column DataFrame (column 'value') indexed by a two-level MultiIndex.

The function must meet these requirements:

  • Build the index with pd.MultiIndex.from_arrays .
  • Do not sort the index. The rows keep the input order.
  • The column name, dtype, level names and row order must match the expected frame exactly, since the result is compared with assert_frame_equal .
  • All three lists have consistent lengths, so no validation is required.

For example:

arrays = [['a', 'a', 'b'], [1, 2, 1]]
names = ['letter', 'num']
values = [10, 20, 30]

build_multiindex_frame(arrays, names, values)
#               value
# letter num
# a      1         10
#        2         20
# b      1         30

What a Strong Answer Covers Guidance

  • How from_arrays pairs the i-th label of each list into one index entry and attaches the level names
  • How the integer list lands in the value column aligned with the index by position
  • Why the row order matches the input, and which operations would reorder it
  • The dtype of the value column and of each level, and what assert_frame_equal compares

Follow-up Questions Guidance

  • How would this differ using from_tuples or from_product ?
  • Why might assert_frame_equal fail even when the values look identical?
  • If values arrived as a pandas Series with its own index, what would the result look like, and how would you fix it?
  • What changes when you select or slice rows on a MultiIndex that is not sorted?