Examples
import numpy as np
import pandas as pd
df = pd.DataFrame({'key': ['K0', 'K2', 'K3', 'K4', 'K5', 'K6'],
'X': ['X0', 'X2', 'X3', 'X4', 'X5', 'X6']})
df
other = pd.DataFrame({'key': ['K0', 'K2', 'K3'],
'Y': ['Y0', 'Y2', 'Y3']})
other
Join DataFrames using their indexes.
df.join(other, lsuffix='_caller', rsuffix='_other')
If you want to join using the key columns, you need to set key to be the index in both df and other.
The joined DataFrame will have key as its index.
df.set_index('key').join(other.set_index('key'))
Another option to join using the key columns is to use the on parameter.
DataFrame.join always uses other’s index but we can use any column in df.
This method preserves the original DataFrame’s index in the result.
df.join(other.set_index('key'), on='key')