Classic Pandas confusion π Let’s break merge vs join vs concat in a way that actually sticks, with when-to-use mental models and examples.
1️⃣ merge() – SQL-style joins (most powerful)
π Think: pd.merge() = SQL JOIN
When to use
You want to join two DataFrames
You have key columns (like
customer_id)You need inner / left / right / outer joins
You want full control
Syntax
pd.merge(left, right, on="id", how="left")
Example
import pandas as pd
orders = pd.DataFrame({
"order_id": [1, 2, 3],
"customer_id": [101, 102, 103]
})
customers = pd.DataFrame({
"customer_id": [101, 102],
"name": ["Alice", "Bob"]
})
result = pd.merge(orders, customers, on="customer_id", how="left")
Output
order_id customer_id name
1 101 Alice
2 102 Bob
3 103 NaN
Key points
✅ Column-based join
✅ Handles column name conflicts (_x, _y)
✅ Best replacement for SQL JOIN
2️⃣ join() – Index-based joining (simpler, cleaner)
π Think: join() = merge on index
When to use
Joining on index
One DataFrame already has the correct index
Cleaner syntax than
merge
Syntax
left.join(right, how="left")
Example
customers = customers.set_index("customer_id")
result = orders.set_index("customer_id").join(customers)
Output
customer_id order_id name
101 1 Alice
102 2 Bob
103 3 NaN
Key points
✅ Index-based join
✅ Less verbose
❌ Not ideal for complex joins
3️⃣ concat() – Stacking, not joining
π Think: concat() = UNION ALL / append
When to use
You want to stack DataFrames
Same schema (or similar)
No key-based matching needed
Syntax
pd.concat([df1, df2], axis=0)
Example (row-wise)
df1 = pd.DataFrame({"id": [1, 2]})
df2 = pd.DataFrame({"id": [3, 4]})
pd.concat([df1, df2])
Output
id
1
2
3
4
Column-wise concat
pd.concat([df1, df2], axis=1)
Key points
✅ Fast and simple
❌ No join logic
❌ Can create NaNs if columns don’t match
π§ Quick decision guide
| Use case | Choose |
|---|---|
| SQL-style join on columns | merge() |
| Join using index | join() |
| Stack rows / columns | concat() |
π₯ One-liner memory trick
merge→ match rows using keysjoin→ merge on indexconcat→ stick things together
Comments
Post a Comment