join()
join is a shortcut for merging on the index.
In this page:
Syntax
joined = df1.join(df2)
joined = df1.join(df2, on="column", how="left")
join()
df1.join(df2) lines up rows by index labels and defaults to a left join. Use on="col" to join df1's column against df2's index. It suits situations where one table is already indexed by the key.
Note:
Use lsuffix and rsuffix when column names overlap.
Example: join()
import pandas as pd
a = pd.DataFrame({"name": ["Ann", "Bob"]}, index=[1, 2])
b = pd.DataFrame({"score": [90, 80]}, index=[2, 3])
print(a.join(b))
print(a.join(b, how="inner"))
# Output:
# name score
# 1 Ann NaN
# 2 Bob 90.0
# name score
# 2 Bob 90
Related Topics
Common Mistakes
- Overlapping column names causing ValueError
- Forgetting join uses the index
- Expecting an inner join by default
Chapter Summary
- join merges on the index
- Default is left join
- on= uses a column of the left table
- Overlapping names need suffixes
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: