0
0
Data Analysis Pythondata~15 mins

concat() for stacking DataFrames in Data Analysis Python - Mini Project: Build & Apply

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