← Back to Pandas Course | Chapter 8: Merging & Joining | Lesson 4 of 7

join()

join is a shortcut for merging on the index.

In this page:

  1. join()
Syntax
python
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()

python
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
  1. Overlapping column names causing ValueError
  2. Forgetting join uses the index
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.