Predict the Output: Outer Merge
Intermediate · Predict the output · 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 lessonWhat will this code print?
import pandas as pd
left = pd.DataFrame({'id': [1, 2], 'a': ['x', 'y']})
right = pd.DataFrame({'id': [2, 3], 'b': ['p', 'q']})
result = pd.merge(left, right, on='id', how='outer')
print(len(result))import pandas as pd
left = pd.DataFrame({'id': [1, 2], 'a': ['x', 'y']})
right = pd.DataFrame({'id': [2, 3], 'b': ['p', 'q']})
result = pd.merge(left, right, on='id', how='outer')
print(len(result))
More Pandas Merge and Join exercises
- Inner Merge Two TablesIntermediate
- Left Join to Keep All CustomersIntermediate
- Merge with Different Key NamesIntermediate
- Stack DataFrames with concatIntermediate
- Fix the Bug: Duplicate Column SuffixesIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz