pandasNot Started
Merge & Join DataFrames
Combine two DataFrames on a common key — the pandas equivalent of SQL JOIN
0%
Knowledge0%
Learn & DrillFluency0%
Drill & SpeedRetention0%
Mastery & ReviewConfidence0%
All modesFree tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge
Fluency
Retention
Knowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
pd.merge() combines two DataFrames based on a common column (like SQL JOIN).
python
orders = pd.DataFrame({'order_id':[1,2,3],'customer_id':[101,102,101],'amount':[50,80,30]})
customers = pd.DataFrame({'customer_id':[101,102,103],'name':['Alice','Bob','Carol']})
# INNER JOIN (default) — only matching rows
pd.merge(orders, customers, on='customer_id')
# LEFT JOIN — all orders, fill missing customer info with NaN
pd.merge(orders, customers, on='customer_id', how='left')how= parameter:
'inner'— only rows with matches in BOTH (default)'left'— all rows from left DataFrame'right'— all rows from right DataFrame'outer'— all rows from both, NaN where no match
Different column names:
python pd.merge(df1, df2, left_on='user_id', right_on='id')
join() vs merge(): df.join() is a shorthand that joins on the index — less common than merge().
Examples
Left join to keep all orders
how="left" keeps all rows from the left DataFrame
import pandas as pd
orders = pd.DataFrame({'id':[1,2,3],'customer_id':[101,102,999],'amount':[50,80,30]})
customers = pd.DataFrame({'customer_id':[101,102],'name':['Alice','Bob']})
result = pd.merge(orders, customers, on='customer_id', how='left')
print(result)
# id customer_id amount name
# 0 1 101 50 Alice
# 1 2 102 80 Bob
# 2 3 999 30 NaN ← no matchHow well did you understand this?
Next in pandas
Sorting a DataFrame