📖 Scenario: You work in a small company that tracks sales data for different products across several months. The data is stored in a table format, but sometimes you need to change how the data is arranged to better understand it.
🎯 Goal: You will learn how to use pandas stack() and unstack() methods to reshape data. This helps you switch between wide and long formats of your sales data.
📋 What You'll Learn
Create a pandas DataFrame with sales data for products over months
Create a variable to select a specific month
Use stack() to convert columns into rows
Use unstack() to convert rows back into columns
Print the final reshaped data
💡 Why This Matters
🌍 Real World
In real companies, sales data often comes in wide tables. Reshaping data with stack and unstack helps prepare data for reports and charts.
💼 Career
Data analysts and scientists frequently reshape data to clean and analyze it. Knowing stack and unstack is a basic skill for data manipulation.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with the following data: products as the index 'Product A', 'Product B', 'Product C' and columns as months 'Jan', 'Feb', 'Mar' with these values exactly: Jan: 100, 150, 200 Feb: 110, 160, 210 Mar: 120, 170, 220
Pandas
Hint
Use pd.DataFrame with a dictionary for columns and index for product names.
2
Select the month to analyze
Create a variable called month and set it to the string 'Feb' to select the sales data for February.
Pandas
Hint
Just assign the string 'Feb' to the variable month.
3
Use stack() and unstack() to reshape data
Use stack() on the sales DataFrame and save the result in a variable called stacked_sales. Then use unstack() on stacked_sales and save it in a variable called unstacked_sales.
Pandas
Hint
Call stack() on sales and assign it to stacked_sales. Then call unstack() on stacked_sales and assign it to unstacked_sales.
4
Print the reshaped data
Print the stacked_sales variable, then print the unstacked_sales variable.
Pandas
Hint
Use two print statements: print(stacked_sales) and print(unstacked_sales.loc['Product A']).
Practice
(1/5)
1. What does the stack() function do in pandas?
easy
A. It filters rows based on a condition.
B. It moves rows into columns, making the data wider.
C. It moves columns into rows, making the data longer.
D. It sorts the DataFrame by index.
Solution
Step 1: Understand the purpose of stack()
stack() takes the columns of a DataFrame and stacks them into the row index, making the data longer.
Step 2: Compare with other options
unstack() does the opposite by moving rows into columns. Sorting and filtering are unrelated to stack().
Final Answer:
It moves columns into rows, making the data longer. -> Option C
Quick Check:
stack() = columns to rows [OK]
Hint: Remember: stack makes data longer by moving columns down [OK]
Common Mistakes:
Confusing stack() with unstack()
Thinking stack() sorts data
Assuming stack() filters rows
2. Which of the following is the correct syntax to unstack a DataFrame named df?
easy
A. df.stack()
B. df.unstack()
C. df.unstacked()
D. df.stack(unstack=True)
Solution
Step 1: Recall the method names
The method to move rows into columns is unstack(), so df.unstack() is correct.
Step 2: Check other options
df.stack() stacks columns into rows. unstacked() does not exist. stack(unstack=True) is invalid syntax.
Final Answer:
df.unstack() -> Option B
Quick Check:
Unstack method is unstack() [OK]
Hint: Use df.unstack() to widen data by moving rows to columns [OK]
A. A Series with index ['A', 'B'] and values [1, 2]
B. A DataFrame with columns 'x' and 'y' and rows 'A' and 'B'
C. A DataFrame with columns 'A' and 'B' and rows 'x' and 'y'
D. A Series with MultiIndex: ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4
Solution
Step 1: Understand stacking on df
Stack moves columns into rows, creating a Series with a MultiIndex combining the original index and column labels.
Step 2: Check the resulting Series
The index pairs are ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4, matching A Series with MultiIndex: ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4.
Final Answer:
A Series with MultiIndex: ('x', 'A')=1, ('x', 'B')=3, ('y', 'A')=2, ('y', 'B')=4 -> Option D
Quick Check:
stack() output is MultiIndex Series [OK]
Hint: Stack creates MultiIndex Series combining rows and columns [OK]