0
0
Pandasdata~30 mins

Setting columns as MultiIndex in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Setting columns as MultiIndex
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(sales) to show the DataFrame with MultiIndex columns.