Merge pandas DataFrames
DataFrame merges combine related records stored in separate tables, similar to a relational database join.
What is Merge pandas DataFrames?
DataFrame merges combine related records stored in separate tables, similar to a relational database join.
Join two pandas DataFrames using a shared key column.
When should you use it?
- Read, clean, and transform tabular data.
- Analyze data from CSV, Excel, APIs, or databases.
- Prepare reports, statistics, and chart data.
Example code
main.py
import pandas as pd
students = pd.DataFrame({"id": [1, 2], "name": ["Ada", "Lin"]})
scores = pd.DataFrame({"id": [1, 2], "score": [95, 88]})
print(students.merge(scores, on="id"))
Expected output
id name score
0 1 Ada 95
1 2 Lin 88
How it works
The inner merge keeps identifiers found in both tables and combines their columns into one result.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Add rows, columns, and missing values.
- Combine filtering, sorting, and groupby into a small report.
- Load a real file, inspect dtypes, and handle invalid data.