What is the main advantage of using a MultiIndex in pandas?
Think about how data can be organized in layers or groups.
MultiIndex lets you have multiple index levels, which helps represent data with nested or grouped categories.
What is the output of this code?
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)
Look at how the MultiIndex tuples are shown as row labels.
The MultiIndex creates two levels of row labels: first level 'A' or 'B', second level 1 or 2, shown hierarchically.
Given this MultiIndex DataFrame, how many rows does it contain?
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))
Count all combinations of the two index levels.
There are 2 options in the first level and 3 in the second, so total rows = 2 * 3 = 6.
What error does this code raise?
import pandas as pd index = pd.MultiIndex.from_tuples([('A', 1), ('B',)]) df = pd.DataFrame({'Value': [10, 20]}, index=index) print(df)
Check if all tuples have the same number of elements.
MultiIndex requires all tuples to have the same length. Here, one tuple has length 2, the other length 1, causing a ValueError.
Given this MultiIndex DataFrame, which option correctly selects all rows where the first index level is 'B'?
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)
Remember how to slice MultiIndex with .loc using tuples.
To select all rows where first level is 'B', use df.loc['B', :]. Option B works but is less explicit; option B is the correct explicit syntax.