📖 Scenario: You work in a small store that tracks sales in two separate lists for two days. You want to combine these lists into one to see all sales together.
🎯 Goal: Combine two sales DataFrames into one using concat() to stack them vertically.
📋 What You'll Learn
Create two DataFrames named sales_day1 and sales_day2 with given data
Create a variable all_sales that stacks the two DataFrames vertically using pd.concat()
Print the combined DataFrame all_sales
💡 Why This Matters
🌍 Real World
Combining sales data from multiple days or stores helps businesses analyze total sales easily.
💼 Career
Data scientists often combine data from different sources to prepare it for analysis or reporting.
Progress0 / 4 steps
1
Create two sales DataFrames
Create a DataFrame called sales_day1 with columns 'Product' and 'Quantity' and these rows: 'Apple', 10 and 'Banana', 5. Also create a DataFrame called sales_day2 with the same columns and these rows: 'Orange', 7 and 'Banana', 3.
Pandas
Hint
Use pd.DataFrame() with a dictionary where keys are column names and values are lists of column values.
2
Set concat axis for stacking
Create a variable called axis and set it to 0 to stack DataFrames vertically using pd.concat().
Pandas
Hint
Use axis = 0 because stacking means adding rows.
3
Stack the DataFrames using concat()
Use pd.concat() with the list [sales_day1, sales_day2] and the variable axis to create a new DataFrame called all_sales that stacks the two DataFrames vertically.
Pandas
Hint
Pass a list of DataFrames to pd.concat() and set axis=0 to stack rows.
4
Print the combined DataFrame
Print the DataFrame all_sales to see the stacked sales data.
Pandas
Hint
Use print(all_sales) to display the combined DataFrame.
Practice
(1/5)
1. What does the concat() function do in pandas?
easy
A. Deletes rows from a DataFrame
B. Joins DataFrames by stacking rows or columns
C. Sorts the DataFrame by index
D. Filters rows based on a condition
Solution
Step 1: Understand the purpose of concat()
concat() is used to combine multiple DataFrames either by stacking rows or columns.
Step 2: Compare with other DataFrame operations
Deleting, sorting, or filtering are different operations and not related to concat().
Final Answer:
Joins DataFrames by stacking rows or columns -> Option B
Quick Check:
concat() stacks DataFrames [OK]
Hint: Remember: concat stacks rows or columns [OK]
Common Mistakes:
Confusing concat with filtering or sorting
Thinking concat deletes data
Assuming concat only merges columns
2. Which of the following is the correct syntax to stack two DataFrames df1 and df2 vertically using concat()?
easy
A. pd.concat(df1 + df2)
B. pd.concat([df1, df2], axis=1)
C. pd.concat(df1, df2)
D. pd.concat([df1, df2], axis=0)
Solution
Step 1: Identify axis for vertical stacking
Vertical stacking means stacking rows, which is done with axis=0.
Step 2: Check syntax correctness
pd.concat() requires a list of DataFrames as first argument, so [df1, df2] is correct.
You want to stack them vertically and keep all columns, filling missing values with NaN. Which code will do this correctly?
hard
A. pd.concat([df1, df2], axis=0, ignore_index=True)
B. pd.concat([df1, df2], axis=1)
C. pd.concat([df1, df2], axis=0).fillna(0)
D. pd.concat([df1, df2], axis=0, join='inner')
Solution
Step 1: Understand vertical stacking with different columns
Stacking vertically with axis=0 combines rows. Since columns differ, missing columns get NaN by default.
Step 2: Check parameters for desired behavior
ignore_index=True resets index. Default join='outer' keeps all columns and fills missing with NaN.
Step 3: Evaluate other options
pd.concat([df1, df2], axis=1) stacks columns horizontally, not vertically. pd.concat([df1, df2], axis=0).fillna(0) fills NaN with 0, not requested. pd.concat([df1, df2], axis=0, join='inner') uses join='inner' which keeps only common columns.
Final Answer:
pd.concat([df1, df2], axis=0, ignore_index=True) -> Option A
Quick Check:
axis=0 + default join='outer' keeps all columns [OK]
Hint: Use axis=0 and default join to keep all columns [OK]
Common Mistakes:
Using axis=1 to stack rows
Using join='inner' and losing columns
Filling NaN when not needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.