Left Join to Keep All Customers
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 lessonPerform a left join of customers and orders on 'customer_id'. Print the number of rows in the result using len().
import pandas as pd
customers = pd.DataFrame({
'customer_id': [1, 2, 3],
'name': ['Alice', 'Bob', 'Charlie']
})
orders = pd.DataFrame({
'customer_id': [1, 1, 2],
'amount': [50, 30, 75]
})
# Left join and print row count
More Pandas Merge and Join exercises
- Inner Merge Two TablesIntermediate
- Merge with Different Key NamesIntermediate
- Stack DataFrames with concatIntermediate
- Predict the Output: Outer MergeIntermediate
- Fix the Bug: Duplicate Column SuffixesIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz