Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df1 = pd.DataFrame({'akey': ['s1', 's2', 's3', 's4'],
                    'value': [2, 3, 4, 5]})
In [3]:
df2 = pd.DataFrame({'bkey': ['s1', 's2', 's3', 's4'],
                    'value': [6, 7, 8, 9]})
df1
Out[3]:
akey value
0 s1 2
1 s2 3
2 s3 4
3 s4 5
In [4]:
df2
Out[4]:
bkey value
0 s1 6
1 s2 7
2 s3 8
3 s4 9

Merge df1 and df2 on the akey and bkey columns. The value columns have the default suffixes, _x and _y, appended.

In [5]:
df1.merge(df2, left_on='akey', right_on='bkey')
Out[5]:
akey value_x bkey value_y
0 s1 2 s1 6
1 s2 3 s2 7
2 s3 4 s3 8
3 s4 5 s4 9

Merge DataFrames df1 and df2 with specified left and right suffixes appended to any overlapping columns.

In [6]:
df1.merge(df2, left_on='akey', right_on='bkey',
          suffixes=('_left', '_right'))
Out[6]:
akey value_left bkey value_right
0 s1 2 s1 6
1 s2 3 s2 7
2 s3 4 s3 8
3 s4 5 s4 9

Merge DataFrames df1 and df2, but raise an exception if the DataFrames have any overlapping columns.

df1.merge(df2, left_on='akey', right_on='bkey', suffixes=(False, False)) Traceback (most recent call last): ... ValueError: columns overlap but no suffix specified: Index(['value'], dtype='object')