Stack DataFrames with concat
Intermediate · Write the code · from Pandas merge(), join(), concat(): Combine DataFrames Like SQL
From the lesson: Pandas merge(), join(), concat(): Combine DataFrames Like SQL
You have a table of customers and a separate table of orders. You need to match each order to the customer who placed it. If you've used SQL, you know this as a JOIN. In pandas, the same idea exists - it's called merge().
Read the lessonConcatenate q1 and q2 DataFrames vertically (stacking rows) with ignore_index=True. Print the shape of the result.
import pandas as pd
q1 = pd.DataFrame({
'month': ['Jan', 'Feb', 'Mar'],
'revenue': [1000, 1200, 1100]
})
q2 = pd.DataFrame({
'month': ['Apr', 'May', 'Jun'],
'revenue': [1300, 1400, 1250]
})
# Concatenate and print shape
More Pandas Merge and Join exercises
- Inner Merge Two TablesIntermediate
- Left Join to Keep All CustomersIntermediate
- Merge with Different Key NamesIntermediate
- Predict the Output: Outer MergeIntermediate
- Fix the Bug: Duplicate Column SuffixesIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz