join() (Pandas)

df.join() combines DataFrames primarily on their index, acting as a convenient shorthand around merge for index-aligned joins. By default it does a left join.

Pandas

join() exists because joining on the index is so common. df.join(other) lines the two frames up by index and glues their columns together, defaulting to how='left' so every row of the calling frame is kept. You can still join on a column by passing on='colname'.

Its biggest convenience is joining many frames at once: df.join([df2, df3]) chains several index-aligned frames in a single call, which merge cannot do as cleanly. If column names collide, supply lsuffix and rsuffix to disambiguate them.

Choose join when your keys already live in the index and merge when you need full control over join columns, how, and validation.

Syntax

df.join(other, on=None, how='left', lsuffix='', rsuffix='')

Examples

Python

import pandas as pd
a = pd.DataFrame({"x": [1, 2]}, index=["p", "q"])
b = pd.DataFrame({"y": [3, 4]}, index=["p", "q"])
print(a.join(b))

Output:

   x  y
p  1  3
q  2  4

Learn it in a lesson

See also

Back to the Dictionary