Data lives in separate tables — students and programmes, orders and customers. merge joins two DataFrames on a shared key, exactly like a SQL join, so you can analyse across them.
merge
students = pd.DataFrame({"name": ["Ana", "Ravi"], "prog_id": [1, 2]})progs = pd.DataFrame({"prog_id": [1, 2], "prog_name": ["Data", "Web"]})pd.merge(students, progs, on="prog_id", how="inner")
Join types
| how= | Keeps |
|---|---|
"inner" | Only rows matching in both (default) |
"left" | All left rows + matches (NaN where none) |
"right" | All right rows + matches |
"outer" | All rows from both, matched where possible |
Key takeaways
- 01merge joins two DataFrames on a shared key — like a SQL join.
- 02how = inner (matches only), left/right (keep one side), outer (keep all).
- 03Duplicated keys multiply rows; unmatched left/right rows produce NaN.
- 04Check the resulting shape; validate= guards against unexpected fan-out.
