📖 Scenario: You work in a small store and keep track of daily sales in tables. Sometimes you get new sales data and want to add it to your existing records.
🎯 Goal: Learn how to combine two sales tables using pandas.concat(), which works like append() but is more flexible and recommended.
📋 What You'll Learn
Create two pandas DataFrames with sales data
Create a list containing these DataFrames
Use pandas.concat() to combine the DataFrames
Print the combined DataFrame
💡 Why This Matters
🌍 Real World
Stores and businesses often get new sales data daily and need to combine it with old data to analyze total sales.
💼 Career
Data analysts and scientists frequently combine multiple datasets to prepare data for reports and insights.
Progress0 / 4 steps
1
Create two sales DataFrames
Import pandas as pd. Create a DataFrame called sales_jan with columns 'Product' and 'Quantity' and these rows: 'Apple', 10, 'Banana', 15. Create another DataFrame called sales_feb with the same columns and these rows: 'Apple', 7, 'Orange', 12.
Pandas
Hint
Use pd.DataFrame() with a dictionary for columns and lists for rows.
2
Create a list of DataFrames
Create a list called sales_list that contains the two DataFrames sales_jan and sales_feb.
Pandas
Hint
Put the two DataFrames inside square brackets to make a list.
3
Combine DataFrames using concat
Use pd.concat() with the list sales_list to create a new DataFrame called all_sales that stacks the rows from both DataFrames.
Pandas
Hint
Use pd.concat() with the list of DataFrames to combine them.
4
Print the combined DataFrame
Print the DataFrame all_sales to see the combined sales data.
Pandas
Hint
Use print(all_sales) to show the combined table.
Practice
(1/5)
1. What is the main reason to use pandas.concat() instead of DataFrame.append()?
easy
A. concat() is faster for single DataFrame addition only
B. Because append() is deprecated and concat() can join multiple DataFrames at once
C. append() works only for columns, not rows
D. concat() automatically sorts columns alphabetically
Solution
Step 1: Understand the deprecation of append()
The append() method is deprecated in pandas and will be removed in future versions.
Step 2: Recognize concat() advantages
concat() can join multiple DataFrames at once, not just one, making it more flexible.
Final Answer:
Because append() is deprecated and concat() can join multiple DataFrames at once -> Option B
Quick Check:
append() deprecated, concat() multi-join [OK]
Hint: Remember: concat replaces append and handles many DataFrames [OK]
Common Mistakes:
Thinking append is still recommended
Believing concat only works for columns
Assuming append is faster than concat
2. Which of the following is the correct syntax to replace df1.append(df2, ignore_index=True) using concat?
easy
A. pd.concat([df1, df2], ignore_index=True)
B. pd.concat([df1, df2], axis=0)
C. pd.concat(df1, df2, ignore_index=True)
D. pd.concat([df1, df2], axis=1, ignore_index=True)
Solution
Step 1: Identify the correct function call
pd.concat() requires a list of DataFrames as the first argument, so [df1, df2] is correct.
Step 2: Use ignore_index=True to reset row numbers
To mimic append(ignore_index=True), pass ignore_index=True to concat(). The default axis is 0 (rows), so no need to specify.
Final Answer:
pd.concat([df1, df2], ignore_index=True) -> Option A
Quick Check:
List of DataFrames + ignore_index=True = correct concat syntax [OK]
Hint: Use list of DataFrames and ignore_index=True with concat [OK]
Common Mistakes:
Passing DataFrames without list brackets
Using axis=1 to add rows (wrong axis)
Omitting ignore_index when needed
3. Given the code:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'A': [3, 4]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)
What will be the output?
medium
A. A
0 1
1 2
0 3
1 4
B. A
0 3
1 4
C. A
0 1
1 2
2 3
3 4
D. Error because ignore_index is not valid
Solution
Step 1: Understand concat with ignore_index=True
concat joins rows from both DataFrames and resets the index to 0,1,2,...
Step 2: Combine rows and reset index
Rows from df1 (1,2) and df2 (3,4) stack vertically with new index 0 to 3.
Final Answer:
A
0 1
1 2
2 3
3 4 -> Option C
Quick Check:
concat stacks rows and resets index with ignore_index=True [OK]