Editor

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 lesson

What 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

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz