0
0
Data Analysis Pythondata~20 mins

MultiIndex (hierarchical indexing) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MultiIndex Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this MultiIndex selection?

Given the DataFrame with a MultiIndex, what will be the output of selecting data.loc['B']?

Data Analysis Python
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
1     30
2     40
B
Value
1    30
2    40
C
Value
B    30
B    40
DKeyError: 'B'
Attempts:
2 left
💡 Hint

Remember that selecting with a level value returns a DataFrame or Series indexed by the remaining level(s).

data_output
intermediate
2:00remaining
How many rows are in the DataFrame after slicing with .loc?

Given the MultiIndex DataFrame below, how many rows remain after slicing with data.loc[('A', slice(1,2))]?

Data Analysis Python
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('A', 3), ('B', 1)])
data = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index)
sliced = data.loc[('A', slice(1,2))]
print(len(sliced))
A1
B2
C3
D4
Attempts:
2 left
💡 Hint

Slice selects rows where the second level is between 1 and 2 inclusive.

visualization
advanced
3:00remaining
Which plot shows the correct aggregation by first level of MultiIndex?

Given a MultiIndex DataFrame with sales data, which plot correctly shows the total sales summed by the first level of the index?

Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt

index = pd.MultiIndex.from_tuples([('Store1', 'Jan'), ('Store1', 'Feb'), ('Store2', 'Jan'), ('Store2', 'Feb')])
data = pd.DataFrame({'Sales': [100, 150, 200, 250]}, index=index)

agg = data.groupby(level=0).sum()
agg.plot(kind='bar')
plt.show()
AScatter plot with points for each sale value
BLine plot with four points labeled Store1-Jan, Store1-Feb, Store2-Jan, Store2-Feb
CPie chart with four slices for each month
DBar plot with two bars labeled Store1 and Store2 showing heights 250 and 450
Attempts:
2 left
💡 Hint

Grouping by level=0 sums sales per store.

🔧 Debug
advanced
2:00remaining
What error does this MultiIndex slicing code raise?

What error will this code raise when trying to slice a MultiIndex DataFrame?

Data Analysis Python
import pandas as pd

index = pd.MultiIndex.from_tuples([('X', 1), ('X', 2), ('Y', 1)])
data = pd.DataFrame({'Value': [5, 10, 15]}, index=index)

result = data.loc['X', 1:2]
print(result)
AIndexingError: Too many indexers
BKeyError: 1
CTypeError: unhashable type: 'slice'
DNo error, prints the sliced DataFrame
Attempts:
2 left
💡 Hint

Check how .loc handles multiple indexers with MultiIndex.

🚀 Application
expert
3:00remaining
What is the resulting DataFrame after swapping levels and sorting?

Given a MultiIndex DataFrame, what is the output after swapping the index levels and sorting by the new index?

Data Analysis Python
import pandas as pd

index = pd.MultiIndex.from_tuples([('C', 2), ('A', 1), ('B', 3)])
data = pd.DataFrame({'Score': [30, 10, 20]}, index=index)

swapped = data.swaplevel(0, 1).sort_index()
print(swapped)
A
       Score
A 1    10
B 3    20
C 2    30
B
       Score
1     10
2     30
3     20
C
       Score
1 A     10
2 C     30
3 B     20
DKeyError: 0
Attempts:
2 left
💡 Hint

Swapping levels changes the order of index levels, then sorting arranges by the new first level.