Working with MultiIndex (hierarchical indexing) in pandas
📖 Scenario: You work in a small store that sells fruits in different cities. You have sales data for each fruit in each city for two months. You want to organize this data so you can easily find sales by city and fruit.
🎯 Goal: You will create a pandas DataFrame with a MultiIndex using city and fruit. Then you will select sales data for a specific city and fruit using the MultiIndex.
📋 What You'll Learn
Create a pandas DataFrame with MultiIndex from city and fruit
Use a tuple list to create the MultiIndex
Select sales data for a specific city and fruit using .loc
Print the selected sales data
💡 Why This Matters
🌍 Real World
Stores and businesses often have sales data organized by multiple categories like location and product. MultiIndex helps organize and access this data easily.
💼 Career
Data analysts and scientists use MultiIndex in pandas to handle complex datasets with multiple levels of grouping, making data analysis more efficient.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales_data with these exact entries: ('New York', 'Apple'): [100, 120], ('New York', 'Banana'): [90, 110], ('Los Angeles', 'Apple'): [80, 95], ('Los Angeles', 'Banana'): [70, 85]. The lists represent sales for two months.
Data Analysis Python
Hint
Use tuples as keys in the dictionary to represent city and fruit pairs.
2
Create the MultiIndex and DataFrame
Create a variable called index using pd.MultiIndex.from_tuples() with the keys of sales_data. Then create a DataFrame called df with the values of sales_data, the index as index, and columns named ['Month 1', 'Month 2'].
Data Analysis Python
Hint
Use list(sales_data.values()) to get the data for the DataFrame.
3
Select sales data for New York Apples
Use df.loc with the tuple ('New York', 'Apple') to select the sales data for apples in New York. Store this in a variable called ny_apple_sales.
Data Analysis Python
Hint
Use df.loc[('New York', 'Apple')] to get the row for New York apples.
4
Print the selected sales data
Print the variable ny_apple_sales to display the sales for apples in New York.
Data Analysis Python
Hint
Use print(ny_apple_sales) to show the sales data.
Practice
(1/5)
1. What is the main purpose of a MultiIndex in pandas?
easy
A. To create plots automatically
B. To label data with multiple levels for better organization
C. To convert data into a list
D. To speed up numerical calculations
Solution
Step 1: Understand MultiIndex concept
MultiIndex allows multiple levels of labels on rows or columns, organizing complex data.
Step 2: Compare options
Only To label data with multiple levels for better organization correctly describes MultiIndex's purpose; others describe unrelated features.
Final Answer:
To label data with multiple levels for better organization -> Option B
Quick Check:
MultiIndex = multiple levels labeling [OK]
Hint: MultiIndex means multiple levels of labels [OK]
Common Mistakes:
Thinking MultiIndex speeds up calculations
Confusing MultiIndex with plotting tools
Believing MultiIndex converts data to lists
2. Which of the following is the correct way to create a MultiIndex from tuples in pandas?
easy
A. pd.MultiIndex.build_tuples([('A', 1), ('B', 2)])
B. pd.MultiIndex.create_tuples([('A', 1), ('B', 2)])
C. pd.MultiIndex.make_tuples([('A', 1), ('B', 2)])
D. pd.MultiIndex.from_tuples([('A', 1), ('B', 2)])
Solution
Step 1: Recall pandas MultiIndex creation methods
The correct method to create MultiIndex from tuples is from_tuples.
Step 2: Check method names
Only pd.MultiIndex.from_tuples([('A', 1), ('B', 2)]) uses the correct method name; others are invalid pandas methods.
Final Answer:
pd.MultiIndex.from_tuples([('A', 1), ('B', 2)]) -> Option D
Quick Check:
Use from_tuples to create MultiIndex from tuples [OK]
Hint: Use from_tuples to create MultiIndex from tuple list [OK]
Common Mistakes:
Using non-existent methods like create_tuples
Confusing method names with make_tuples or build_tuples
Forgetting to import pandas as pd
3. Given the code:
import pandas as pd
index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1)])
data = pd.Series([10, 20, 30], index=index)
print(data.loc['A'])
What is the output?
medium
A. A 10
A 20
dtype: int64
B. 10
20
C. 1 10
2 20
dtype: int64
D. KeyError
Solution
Step 1: Understand MultiIndex and selection
The MultiIndex has two levels: first level 'A' or 'B', second level numbers. Selecting data.loc['A'] returns all entries where first level is 'A'.
Step 2: Check values for 'A'
Entries with first level 'A' are ('A', 1):10 and ('A', 2):20, so output shows index 1 and 2 with values 10 and 20.
Final Answer:
1 10
2 20
dtype: int64 -> Option C
Quick Check:
data.loc['A'] returns second level index and values [OK]
Hint: Selecting first level returns second level index and values [OK]
Common Mistakes:
Expecting full tuple index in output
Getting KeyError by wrong indexing
Confusing index labels with values
4. What is wrong with this code snippet?
import pandas as pd
index = pd.MultiIndex.from_product([['X', 'Y'], [1, 2]])
data = pd.Series([5, 10, 15], index=index)
print(data)
medium
A. The length of data does not match the MultiIndex length
B. from_product is not a valid MultiIndex method
C. Series cannot have MultiIndex
D. The index should be a list, not MultiIndex
Solution
Step 1: Check MultiIndex length
from_product creates all combinations: 2 (X,Y) * 2 (1,2) = 4 index entries.
Step 2: Check data length
Data has length 3, which does not match 4 index entries, causing a ValueError.
Final Answer:
The length of data does not match the MultiIndex length -> Option A
Quick Check:
Data length must match MultiIndex length [OK]
Hint: Data length must equal MultiIndex length [OK]
Common Mistakes:
Thinking from_product is invalid
Believing Series can't have MultiIndex
Assuming index must be list, not MultiIndex
5. You have sales data for two stores over two years stored as a MultiIndex DataFrame with levels ['Store', 'Year']. How do you select sales data for Store 'B' in year 2023?
hard
A. df.loc[('B', 2023)]
B. df['B'][2023]
C. df.loc[2023, 'B']
D. df.loc['B', [2023]]
Solution
Step 1: Understand MultiIndex selection syntax
To select a row with multiple index levels, use a tuple inside loc with all levels specified.
Step 2: Analyze options
df.loc[('B', 2023)] correctly specifies both levels as a tuple. df.loc[2023, 'B'] uses incorrect order of levels (year before store). df['B'][2023] attempts column access first, causing KeyError. df.loc['B', [2023]] uses a list which pandas interprets as column slice, causing KeyError if no such columns.
Final Answer:
df.loc[('B', 2023)] -> Option A
Quick Check:
Use tuple in loc for MultiIndex row selection [OK]
Hint: Use tuple inside loc to select MultiIndex row [OK]
Common Mistakes:
Reversing the order of index levels
Using df['B'] to access rows instead of loc with tuple
Passing list to loc causing column slice misinterpretation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.