Editor

Inner Merge Two Tables

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 the students and grades DataFrames on 'student_id' using an inner join. Print the resulting names and grades as a list of lists using .values.tolist().

import pandas as pd
students = pd.DataFrame({
'student_id': [1, 2, 3, 4],
'name': ['Alice', 'Bob', 'Charlie', 'Diana']
})
grades = pd.DataFrame({
'student_id': [1, 2, 4],
'grade': ['A', 'B', 'A']
})
# Inner merge and print name + grade

More Pandas Merge and Join exercises

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