📖 Scenario: You work in a small store and keep sales records for two different months separately. Now, you want to combine these records to see all sales together.
🎯 Goal: Learn how to stack two sales records DataFrames vertically using concat() to get a combined sales record.
📋 What You'll Learn
Create two DataFrames named sales_jan and sales_feb with given sales data
Create a list named sales_list containing both DataFrames
Use pd.concat() with sales_list to stack the DataFrames vertically into all_sales
Print the all_sales DataFrame to see the combined sales
💡 Why This Matters
🌍 Real World
Combining monthly or weekly sales data into one table for easier analysis and reporting.
💼 Career
Data analysts often need to merge data from multiple sources or time periods to get a complete picture.
Progress0 / 4 steps
1
Create two sales DataFrames
Create a DataFrame called sales_jan with columns 'Product' and 'Quantity' and these rows: 'Apple', 10, 'Banana', 15. Also create a DataFrame called sales_feb with the same columns and these rows: 'Apple', 7, 'Orange', 12. Use pd.DataFrame() and pass a list of dictionaries.
Data Analysis Python
Hint
Use pd.DataFrame with a list of dictionaries. Each dictionary is a row with keys as column names.
2
Create a list of the two DataFrames
Create a list called sales_list that contains the two DataFrames sales_jan and sales_feb in that order.
Data Analysis Python
Hint
Use square brackets to create a list with the two DataFrames inside.
3
Stack the DataFrames vertically using concat()
Use pd.concat() with the list sales_list to stack the two DataFrames vertically. Assign the result to a new variable called all_sales.
Data Analysis Python
Hint
Call pd.concat() with the list of DataFrames as the argument.
4
Print the combined DataFrame
Print the DataFrame all_sales to see the stacked sales records from January and February.
Data Analysis Python
Hint
Use print(all_sales) to display the combined DataFrame.
Practice
(1/5)
1. What does the concat() function do when stacking DataFrames with axis=0?
easy
A. It sorts the DataFrames before stacking.
B. It stacks DataFrames horizontally by adding columns.
C. It stacks DataFrames vertically by adding rows.
D. It merges DataFrames based on matching column values.
Solution
Step 1: Understand axis parameter in concat()
The axis=0 means stacking along rows, so new rows are added below existing ones.
Step 2: Differentiate vertical vs horizontal stacking
Vertical stacking adds rows; horizontal stacking (axis=1) adds columns side by side.
Final Answer:
It stacks DataFrames vertically by adding rows. -> Option C
Quick Check:
axis=0 means vertical stacking [OK]
Hint: axis=0 adds rows, axis=1 adds columns [OK]
Common Mistakes:
Confusing axis=0 with horizontal stacking
Thinking concat merges like join
Assuming concat sorts data automatically
2. Which of the following is the correct syntax to stack two DataFrames df1 and df2 vertically and reset the index?
easy
A. pd.concat([df1, df2], axis=1, ignore_index=True)
B. pd.concat([df1, df2], axis=0, ignore_index=True)
C. pd.concat(df1, df2, axis=0, ignore_index=True)
D. pd.concat([df1, df2], axis=0)
Solution
Step 1: Check the concat() function syntax
Correct syntax requires a list of DataFrames as first argument, e.g., [df1, df2].
Step 2: Confirm axis and ignore_index usage
To stack vertically and reset index, use axis=0 and ignore_index=True.
Final Answer:
pd.concat([df1, df2], axis=0, ignore_index=True) -> Option B
Quick Check:
List + axis=0 + ignore_index=True = correct syntax [OK]
Hint: Use list of DataFrames and axis=0 for vertical concat [OK]