📖 Scenario: You work in a small company that tracks sales data. The data is organized by product categories and months. You want to organize the columns in a table to show this hierarchy clearly.
🎯 Goal: Create a pandas DataFrame with sales data, then set the columns as a MultiIndex to group by product category and month.
📋 What You'll Learn
Create a pandas DataFrame with sales data for two product categories and two months.
Create a MultiIndex for the columns using product categories and months.
Set the DataFrame columns to this MultiIndex.
Print the DataFrame to see the MultiIndex columns.
💡 Why This Matters
🌍 Real World
Organizing sales or financial data with multiple categories helps businesses analyze performance clearly.
💼 Career
Data analysts and scientists often use MultiIndex in pandas to handle complex datasets with hierarchical relationships.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd. Create a DataFrame called sales with these columns and values: 'Product A_Jan': [100, 150], 'Product A_Feb': [120, 130], 'Product B_Jan': [90, 110], 'Product B_Feb': [95, 105]. Use index labels ['Store 1', 'Store 2'].
Pandas
Hint
Use pd.DataFrame with a dictionary for columns and a list for the index.
2
Create the MultiIndex for columns
Create a MultiIndex called multi_cols from the list of tuples: [('Product A', 'Jan'), ('Product A', 'Feb'), ('Product B', 'Jan'), ('Product B', 'Feb')]. Use pd.MultiIndex.from_tuples().
Pandas
Hint
Use pd.MultiIndex.from_tuples() with the list of tuples for product and month.
3
Set the DataFrame columns to the MultiIndex
Set the sales.columns to the MultiIndex variable multi_cols.
Pandas
Hint
Assign multi_cols to sales.columns.
4
Print the DataFrame with MultiIndex columns
Print the sales DataFrame to display the columns as a MultiIndex.
Pandas
Hint
Use print(sales) to show the DataFrame with MultiIndex columns.
Practice
(1/5)
1. What is the main purpose of setting columns as a MultiIndex in a pandas DataFrame?
easy
A. To sort columns alphabetically
B. To convert columns into rows
C. To remove duplicate columns
D. To organize columns with multiple header levels for better grouping
Solution
Step 1: Understand MultiIndex columns
MultiIndex columns allow multiple levels of headers, grouping related columns together.
Step 2: Identify the purpose
This organization helps in better data analysis and clarity by grouping related columns under common headers.
Final Answer:
To organize columns with multiple header levels for better grouping -> Option D
Quick Check:
MultiIndex = multiple header levels [OK]
Hint: MultiIndex groups columns under multiple headers [OK]
Common Mistakes:
Confusing MultiIndex with row indexing
Thinking MultiIndex removes duplicates
Assuming MultiIndex sorts columns automatically
2. Which of the following is the correct syntax to set MultiIndex columns from a list of tuples cols in pandas?
easy
A. df.columns = pd.MultiIndex.from_tuples(cols)
B. df.columns = pd.MultiIndex(cols)
C. df.columns = MultiIndex.from_tuples(cols)
D. df.columns = pd.MultiIndex.tuples(cols)
Solution
Step 1: Recall the correct method
The pandas method to create MultiIndex from tuples is pd.MultiIndex.from_tuples().