Editor

Merge with Different Key Names

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 lesson

Merge products and inventory where the key is called 'prod_id' in products and 'product_id' in inventory. Print the product names and their stock levels as a list of lists.

import pandas as pd
products = pd.DataFrame({
'prod_id': [1, 2, 3],
'name': ['Laptop', 'Mouse', 'Keyboard']
})
inventory = pd.DataFrame({
'product_id': [1, 2, 3],
'stock': [45, 200, 150]
})
# Merge using left_on and right_on

More Pandas Merge and Join exercises

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