0
0
Pandasdata~20 mins

Selecting data with MultiIndex in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MultiIndex Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
   value
0     30
1     40
B
       value
B 1     30
  2     40
CKeyError: 'B'
D
   value
1     30
2     40
Attempts:
2 left
💡 Hint
Use .loc with the first level key to select all rows under that level.
data_output
intermediate
2: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)
AKeyError: 2
B
   value
A     20
B     40
C
   value
A 2   20
B 2   40
D
   value
1     20
2     40
Attempts:
2 left
💡 Hint
The .xs() method selects data at a particular level value across the other level.
🔧 Debug
advanced
2: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)
AReturns rows with first level 'X' and all second level values
BTypeError: unhashable type: 'tuple'
CKeyError: ('X',)
DIndexError: too many indices
Attempts:
2 left
💡 Hint
Check how tuples are used to select MultiIndex rows with .loc.
🚀 Application
advanced
2: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
Adata.loc[('A', slice(2, 3))]
Bdata.loc[('A', [2, 3])]
Cdata.loc['A', 2:3]
Ddata.loc[('A', 2:3)]
Attempts:
2 left
💡 Hint
Use slice objects inside .loc for range selection on MultiIndex levels.
🧠 Conceptual
expert
2:00remaining
Understanding MultiIndex partial indexing behavior
Which statement best describes the behavior of partial indexing with .loc on a MultiIndex DataFrame?
APartial indexing with .loc selects all rows matching the provided prefix of index levels.
BPartial indexing with .loc requires specifying all levels of the MultiIndex exactly.
CPartial indexing with .loc always returns a Series, never a DataFrame.
DPartial indexing with .loc raises an error if fewer levels than the MultiIndex are specified.
Attempts:
2 left
💡 Hint
Think about how .loc behaves when given fewer keys than the number of index levels.