Merge datafram by joining on a column

In [1]:
import pandas as pd

In [2]:
df = pd.DataFrame([[1, 3], [2, 4]], columns=['A', 'B'])
df


Out[2]:
A B
0 1 3
1 2 4

In [3]:
df2 = pd.DataFrame([[1, 5], [1, 6]], columns=['A', 'C'])
df2


Out[3]:
A C
0 1 5
1 1 6

In [4]:
df.merge(df2, how='left', on='A')  # merges on columns A


Out[4]:
A B C
0 1 3 5.0
1 1 3 6.0
2 2 4 NaN

In [5]:
df2.drop_duplicates(subset=['A'], inplace=True)

In [6]:
df.merge(df2, how='left', on='A')


Out[6]:
A B C
0 1 3 5.0
1 2 4 NaN

In [ ]: