0
0
Pandasdata~15 mins

concat() for stacking DataFrames in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Stacking DataFrames using concat()
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(all_sales) to display the combined DataFrame.