Skip to main content
Albero Academy

Merging & Joining

Combine DataFrames on a key — exactly like SQL joins.

Aggregation & Combining10 min

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

join on a key
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

  1. 01merge joins two DataFrames on a shared key — like a SQL join.
  2. 02how = inner (matches only), left/right (keep one side), outer (keep all).
  3. 03Duplicated keys multiply rows; unmatched left/right rows produce NaN.
  4. 04Check the resulting shape; validate= guards against unexpected fan-out.

Finished this chapter?

Mark it complete to track your progress through Pandas.