Challenge - 5 Problems
MultiIndex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Selecting rows by level value in MultiIndex
What is the output of this code snippet that selects rows where the first level of the MultiIndex is 'B'?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)]) data = pd.DataFrame({'value': [10, 20, 30, 40]}, index=index) result = data.loc['B'] print(result)
Attempts:
2 left
💡 Hint
Use .loc with the first level key to select all rows under that level.
✗ Incorrect
Using .loc['B'] on a MultiIndex DataFrame selects all rows where the first level index is 'B'. The resulting DataFrame has the second level index as its index.
❓ data_output
intermediate2:00remaining
Selecting data with .xs() method
Given the MultiIndex DataFrame below, what is the output of selecting data with .xs(2, level=1)?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)]) data = pd.DataFrame({'value': [10, 20, 30, 40]}, index=index) result = data.xs(2, level=1) print(result)
Attempts:
2 left
💡 Hint
The .xs() method selects data at a particular level value across the other level.
✗ Incorrect
Using .xs(2, level=1) selects all rows where the second level index is 2, returning a DataFrame indexed by the first level.
🔧 Debug
advanced2:00remaining
Why does this MultiIndex selection raise an error?
What error does this code raise and why?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('X', 'a'), ('X', 'b'), ('Y', 'a'), ('Y', 'b')]) data = pd.DataFrame({'val': [1, 2, 3, 4]}, index=index) result = data.loc[('X',)] print(result)
Attempts:
2 left
💡 Hint
Check how tuples are used to select MultiIndex rows with .loc.
✗ Incorrect
Using .loc with a tuple containing only one element ('X',) looks for an exact match of that tuple in the index, which does not exist, causing a KeyError.
🚀 Application
advanced2:00remaining
Selecting multiple levels with .loc using slice
Given this MultiIndex DataFrame, which option correctly selects all rows where the first level is 'A' and the second level is between 2 and 3 inclusive?
Pandas
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('A', 3), ('B', 2), ('B', 3)]) data = pd.DataFrame({'val': [10, 20, 30, 40, 50]}, index=index) # Select rows here
Attempts:
2 left
💡 Hint
Use slice objects inside .loc for range selection on MultiIndex levels.
✗ Incorrect
Using data.loc[('A', slice(2, 3))] correctly selects rows with first level 'A' and second level between 2 and 3 inclusive.
🧠 Conceptual
expert2:00remaining
Understanding MultiIndex partial indexing behavior
Which statement best describes the behavior of partial indexing with .loc on a MultiIndex DataFrame?
Attempts:
2 left
💡 Hint
Think about how .loc behaves when given fewer keys than the number of index levels.
✗ Incorrect
Partial indexing allows selecting all rows that match the given prefix of the MultiIndex levels, returning all matching rows.