Data Science ยท Chapter 12 of 43
Merging & Joining
Combine tables with `merge` (like SQL JOIN) or `concat` (stack rows/columns).
By default `merge` does an INNER join on shared column names.
Example 1 (python)
import pandas as pd
customers = pd.read_csv('customers.csv')
orders = pd.read_csv('orders.csv')
df = customers.merge(orders, on='customer_id', how='left')Left join to keep all customers.
Example 2 (python)
df = pd.concat([df1, df2], ignore_index=True)Stack DataFrames vertically.
Key points
- merge = SQL JOIN.
- how = 'inner'|'left'|'right'|'outer'.
- concat stacks rows/columns.
- Watch for duplicate columns after joining.
๐ก Note: Always sanity-check row counts before and after a merge โ accidental many-to-many joins can explode your data.
