0
0
Pandasdata~20 mins

Why MultiIndex enables hierarchical data in Pandas - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
1:30remaining
Understanding MultiIndex structure

What is the main advantage of using a MultiIndex in pandas?

AIt allows storing multiple levels of indexing, enabling hierarchical data representation.
BIt automatically sorts data without any user input.
CIt converts all data into a flat table with no hierarchy.
DIt removes duplicate rows from the DataFrame.
Attempts:
2 left
💡 Hint

Think about how data can be organized in layers or groups.

Predict Output
intermediate
2:00remaining
Output of MultiIndex DataFrame

What is the output of this code?

Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1)])
df = pd.DataFrame({'Value': [10, 20, 30]}, index=index)
print(df)
AError: MultiIndex not supported in DataFrame
B
  Value
A 1 10
A 2 20
B 1 30
C
Value
A 10
A 20
B 30
D
       Value
A 1      10
  2      20
B 1      30
Attempts:
2 left
💡 Hint

Look at how the MultiIndex tuples are shown as row labels.

data_output
advanced
1:30remaining
Number of rows in MultiIndex DataFrame

Given this MultiIndex DataFrame, how many rows does it contain?

Pandas
import pandas as pd

index = pd.MultiIndex.from_product([['X', 'Y'], [1, 2, 3]])
df = pd.DataFrame({'Score': [5, 6, 7, 8, 9, 10]}, index=index)
print(len(df))
A6
B3
C9
D2
Attempts:
2 left
💡 Hint

Count all combinations of the two index levels.

🔧 Debug
advanced
2:00remaining
Identify the error in MultiIndex creation

What error does this code raise?

Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('B',)])
df = pd.DataFrame({'Value': [10, 20]}, index=index)
print(df)
ATypeError: cannot create MultiIndex from tuples
BIndexError: tuple index out of range
CValueError: all tuples must be the same length
DNo error, prints DataFrame correctly
Attempts:
2 left
💡 Hint

Check if all tuples have the same number of elements.

🚀 Application
expert
2:30remaining
Selecting data using MultiIndex

Given this MultiIndex DataFrame, which option correctly selects all rows where the first index level is 'B'?

Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('B', 1), ('B', 2), ('C', 1)])
df = pd.DataFrame({'Value': [10, 20, 30, 40]}, index=index)
Adf.loc['B']
Bdf.loc['B', :]
Cdf.loc['B', 1]
Ddf.loc[:, 'B']
Attempts:
2 left
💡 Hint

Remember how to slice MultiIndex with .loc using tuples.