0
0
Data Analysis Pythondata~30 mins

Stack and unstack in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Stack and Unstack in Pandas
📖 Scenario: You work in a small company that tracks sales data for different products across regions. The data is stored in a table format with multi-level columns representing product categories and subcategories.You want to learn how to reshape this data to analyze it better by stacking and unstacking the table.
🎯 Goal: Learn how to use stack() and unstack() methods in pandas to reshape multi-index DataFrames for easier analysis.
📋 What You'll Learn
Create a pandas DataFrame with multi-level columns representing product categories and subcategories.
Create a variable to select a specific level for stacking.
Use the stack() method to reshape the DataFrame by stacking the specified level.
Print the reshaped DataFrame to see the stacked result.
💡 Why This Matters
🌍 Real World
Stacking and unstacking data is useful when you want to convert data between wide and long formats. This helps in preparing data for analysis or visualization.
💼 Career
Data analysts and scientists often reshape data to fit the needs of different tools or to simplify complex datasets for reporting and modeling.
Progress0 / 4 steps
1
Create a DataFrame with Multi-Level Columns
Create a pandas DataFrame called sales with the following data and multi-level columns. The columns should have two levels: the first level is 'Fruit' and 'Vegetable', and the second level is 'Apples', 'Oranges' under 'Fruit', and 'Carrots', 'Potatoes' under 'Vegetable'. The rows are indexed by 'Region' with values 'North' and 'South'. The data values are: North - Apples: 10, Oranges: 15, Carrots: 7, Potatoes: 12; South - Apples: 20, Oranges: 25, Carrots: 14, Potatoes: 18.
Data Analysis Python
Hint

Use pd.MultiIndex.from_tuples to create multi-level columns and pass it to the columns parameter of pd.DataFrame.

2
Set the Level to Stack
Create a variable called level_to_stack and set it to 1 to indicate that you want to stack the second level of the columns (the product names).
Data Analysis Python
Hint

Set level_to_stack to the integer 1 to select the second level of columns.

3
Stack the DataFrame
Use the stack() method on the sales DataFrame with the parameter level=level_to_stack and assign the result to a new variable called stacked_sales.
Data Analysis Python
Hint

Call sales.stack(level=level_to_stack) and assign it to stacked_sales.

4
Print the Stacked DataFrame
Print the variable stacked_sales to display the reshaped DataFrame after stacking.
Data Analysis Python
Hint

Use print(stacked_sales) to show the stacked DataFrame.