Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'key': ['K0', 'K2', 'K3', 'K4', 'K5', 'K6'],
                   'X': ['X0', 'X2', 'X3', 'X4', 'X5', 'X6']})
df
Out[2]:
key X
0 K0 X0
1 K2 X2
2 K3 X3
3 K4 X4
4 K5 X5
5 K6 X6
In [3]:
other = pd.DataFrame({'key': ['K0', 'K2', 'K3'],
                      'Y': ['Y0', 'Y2', 'Y3']})
other
Out[3]:
key Y
0 K0 Y0
1 K2 Y2
2 K3 Y3

Join DataFrames using their indexes.

In [4]:
df.join(other, lsuffix='_caller', rsuffix='_other')
Out[4]:
key_caller X key_other Y
0 K0 X0 K0 Y0
1 K2 X2 K2 Y2
2 K3 X3 K3 Y3
3 K4 X4 NaN NaN
4 K5 X5 NaN NaN
5 K6 X6 NaN NaN

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.

In [5]:
df.set_index('key').join(other.set_index('key'))
Out[5]:
X Y
key
K0 X0 Y0
K2 X2 Y2
K3 X3 Y3
K4 X4 NaN
K5 X5 NaN
K6 X6 NaN

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.

In [6]:
df.join(other.set_index('key'), on='key')
Out[6]:
key X Y
0 K0 X0 Y0
1 K2 X2 Y2
2 K3 X3 Y3
3 K4 X4 NaN
4 K5 X5 NaN
5 K6 X6 NaN