Joining Tables: merge and concat
Data is often split across tables - orders in one, customers in another. merge joins them on a shared column, like a SQL JOIN or VLOOKUP in Excel.
Data is spread across tables: orders here, customers there, prices somewhere else. merge joins them on a shared key, like VLOOKUP or SQL JOIN; concat stacks tables with the same columns, like monthly files.
Example
Kinds of join
how="inner"(default) - only rows found in both tableshow="left"- every row from the left table; blanks where there's no matchhow="right"- every row from the right tablehow="outer"- every row from both
Finding rows with no match
Example
Stacking tables: concat
Example
Before merging, check the key columns have the same type in both tables (for example both numbers). A text "10" won't match a number 10.
Key points
- merge joins on a key: inner, left, right, outer.
- indicator=True shows which rows matched.
- concat stacks tables.
📝 Test yourself
1. Which join keeps every row from the left table?
2. What does pd.concat do?
3. Fill in the column name to join on:
orders.merge(customers, ="cust_id")
