Bird
Raised Fist0
Pandasdata~10 mins

Creating MultiIndex DataFrames in Pandas - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a MultiIndex from two lists.

Pandas
import pandas as pd
levels = [['A', 'B'], ['one', 'two']]
index = pd.MultiIndex.from_tuples([1])
print(index)
Drag options to blanks, or click blank then click option'
A[('A', 'one'), ('A', 'two'), ('B', 'one'), ('B', 'two')]
B[('A', 'one'), ('B', 'two')]
C['A', 'B', 'one', 'two']
D['A', 'one', 'B', 'two']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a flat list instead of a list of tuples.
Not including all combinations of the levels.
2fill in blank
medium

Complete the code to create a MultiIndex using from_product.

Pandas
import pandas as pd
levels = [['X', 'Y'], [1, 2]]
index = pd.MultiIndex.[1](levels)
print(index)
Drag options to blanks, or click blank then click option'
Afrom_tuples
Bfrom_arrays
Cfrom_product
Dfrom_frame
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tuples without providing tuples.
Confusing from_arrays with from_product.
3fill in blank
hard

Fix the error in creating a MultiIndex from arrays.

Pandas
import pandas as pd
arrays = [['a', 'a', 'b', 'b'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays([1])
print(index)
Drag options to blanks, or click blank then click option'
A[arrays]
Barrays
Carrays[0]
Darrays[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Wrapping the arrays list inside another list.
Passing only one array instead of both.
4fill in blank
hard

Fill both blanks to create a DataFrame with a MultiIndex from tuples and set column names.

Pandas
import pandas as pd
tuples = [('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]
index = pd.MultiIndex.[1](tuples, names=[2])
data = [10, 20, 30, 40]
df = pd.DataFrame(data, index=index, columns=['value'])
print(df)
Drag options to blanks, or click blank then click option'
Afrom_tuples
Bfrom_product
C['color', 'number']
D('color', 'number')
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_product instead of from_tuples.
Passing a tuple instead of a list for names.
5fill in blank
hard

Fill all three blanks to create a MultiIndex DataFrame from product, set names, and assign columns.

Pandas
import pandas as pd
levels = [['A', 'B'], [1, 2]]
index = pd.MultiIndex.[1](levels, names=[2])
data = [[5, 10], [15, 20], [25, 30], [35, 40]]
df = pd.DataFrame(data, index=index, columns=[3])
print(df)
Drag options to blanks, or click blank then click option'
Afrom_product
B['level_1', 'level_2']
C['col1', 'col2']
Dfrom_tuples
Attempts:
3 left
💡 Hint
Common Mistakes
Using from_tuples instead of from_product.
Passing tuples instead of lists for names or columns.

Practice

(1/5)
1. What is the main purpose of a MultiIndex DataFrame in pandas?
easy
A. To organize data with multiple levels of row or column labels
B. To store only numeric data efficiently
C. To create plots directly from data
D. To convert DataFrames into lists

Solution

  1. Step 1: Understand MultiIndex concept

    A MultiIndex DataFrame allows multiple levels of labels on rows or columns, helping organize complex data.
  2. Step 2: Compare options

    Options A, C, and D describe unrelated features: numeric storage, plotting, and conversion, not MultiIndex purpose.
  3. Final Answer:

    To organize data with multiple levels of row or column labels -> Option A
  4. Quick Check:

    MultiIndex = multiple label levels [OK]
Hint: MultiIndex means multiple label levels in rows or columns [OK]
Common Mistakes:
  • Confusing MultiIndex with data type storage
  • Thinking MultiIndex is for plotting
  • Believing MultiIndex converts DataFrames to lists
2. Which of the following is the correct way to create a MultiIndex from tuples in pandas?
easy
A. pd.MultiIndex.from_tuples([('A', 1), ('B', 2)])
B. pd.MultiIndex.create_tuples([('A', 1), ('B', 2)])
C. pd.MultiIndex.tuples([('A', 1), ('B', 2)])
D. pd.MultiIndex.make_tuples([('A', 1), ('B', 2)])

Solution

  1. Step 1: Recall pandas MultiIndex creation methods

    The correct method to create a MultiIndex from tuples is pd.MultiIndex.from_tuples().
  2. Step 2: Check method names

    Options A, B, and C use incorrect method names that do not exist in pandas.
  3. Final Answer:

    pd.MultiIndex.from_tuples([('A', 1), ('B', 2)]) -> Option A
  4. Quick Check:

    Use from_tuples() to create MultiIndex from tuples [OK]
Hint: Use from_tuples() method to create MultiIndex from tuple list [OK]
Common Mistakes:
  • Using non-existent methods like create_tuples or make_tuples
  • Confusing from_tuples with from_product
  • Passing wrong data types instead of list of tuples
3. What will be the output of the following code?
import pandas as pd
index = pd.MultiIndex.from_product([['A', 'B'], [1, 2]])
df = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index)
df
medium
A. An error because from_product requires numeric inputs only
B. A DataFrame with a single-level index of ['A', 'B', 1, 2] and values 10, 20, 30, 40
C. A DataFrame with columns 'A', 'B', 1, 2 and values 10, 20, 30, 40
D. A DataFrame with index levels 'A' and 'B' combined with 1 and 2, showing values 10, 20, 30, 40

Solution

  1. Step 1: Understand pd.MultiIndex.from_product

    This method creates a MultiIndex from the Cartesian product of input iterables, here ['A', 'B'] and [1, 2].
  2. Step 2: Analyze DataFrame creation

    The DataFrame uses this MultiIndex as its index and assigns values [10, 20, 30, 40] accordingly, matching the 4 index combinations.
  3. Final Answer:

    A DataFrame with index levels 'A' and 'B' combined with 1 and 2, showing values 10, 20, 30, 40 -> Option D
  4. Quick Check:

    from_product creates MultiIndex from all pairs [OK]
Hint: from_product creates all pairs of index levels [OK]
Common Mistakes:
  • Thinking from_product only accepts numbers
  • Confusing MultiIndex with columns
  • Expecting a single-level index instead of MultiIndex
4. Identify the error in this code snippet that tries to create a MultiIndex DataFrame:
import pandas as pd
index = pd.MultiIndex.from_tuples(['A', 1], ['B', 2])
df = pd.DataFrame({'Value': [100, 200]}, index=index)
print(df)
medium
A. MultiIndex cannot be used as DataFrame index
B. DataFrame values list length does not match index length
C. from_tuples expects a single list of tuples, not two separate lists
D. pd.DataFrame requires columns parameter when using MultiIndex

Solution

  1. Step 1: Check MultiIndex.from_tuples usage

    The method expects one argument: a list of tuples like [('A', 1), ('B', 2)]. The code incorrectly passes two separate lists.
  2. Step 2: Verify DataFrame creation

    Because the MultiIndex creation fails, the DataFrame cannot be created properly. Other options are incorrect because the values list length matches the intended index length, MultiIndex can be used as index, and columns parameter is optional.
  3. Final Answer:

    from_tuples expects a single list of tuples, not two separate lists -> Option C
  4. Quick Check:

    from_tuples needs one list of tuples [OK]
Hint: Pass one list of tuples to from_tuples() [OK]
Common Mistakes:
  • Passing multiple lists instead of one list of tuples
  • Assuming MultiIndex can't be DataFrame index
  • Forgetting to match values length with index length
5. You have sales data for two stores ('Store1', 'Store2') over two years (2022, 2023). How do you create a MultiIndex DataFrame with these as row indices and columns 'Sales' and 'Profit' filled with zeros?
hard
A. Use pd.MultiIndex.from_product([['Sales', 'Profit']], [['Store1', 'Store2'], [2022, 2023]]) as columns and zeros as data
B. Use pd.MultiIndex.from_product([['Store1', 'Store2'], [2022, 2023]]) as index and pd.DataFrame with zeros for columns ['Sales', 'Profit']
C. Use pd.MultiIndex.from_tuples([('Store1', 2022), ('Store2', 2023)]) as index and pd.DataFrame with zeros for columns ['Sales', 'Profit']
D. Create a DataFrame with columns ['Store1', 'Store2', 2022, 2023, 'Sales', 'Profit'] filled with zeros

Solution

  1. Step 1: Create MultiIndex for rows

    Use pd.MultiIndex.from_product with store names and years to get all combinations as row indices.
  2. Step 2: Create DataFrame with zeros and correct columns

    Use the MultiIndex as index and specify columns ['Sales', 'Profit'], filling data with zeros matching the index length.
  3. Final Answer:

    Use pd.MultiIndex.from_product([['Store1', 'Store2'], [2022, 2023]]) as index and pd.DataFrame with zeros for columns ['Sales', 'Profit'] -> Option B
  4. Quick Check:

    from_product for all index combos + zeros DataFrame [OK]
Hint: Use from_product for all index combos, zeros for data [OK]
Common Mistakes:
  • Using from_tuples with incomplete pairs
  • Confusing columns with index levels
  • Creating flat DataFrame without MultiIndex
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard