To merge Python Pandas data frames, we can call the merge
method.
For instance, we write
np.random.seed(0)
left = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': np.random.randn(4)})
right = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': np.random.randn(4)})
m = left.merge(right, on='key')
to create the left
and right
dataframes with some random values.
Then we call left.merge
with right
and set on
to 'key'
to merge the rows by the key
column value.
This will do an inner join.
We can also add the how
argument to merge
to do other kinds of joins.
So we can write
left.merge(right, on='key', how='left')
to set how
to 'left'
to do a left join.
We can also set how
to 'right'
or 'outer'
to do those joins.