Joining Data with pandas
Aaren Stubberfield
Instructor
.merge()
method:
on
, left_on
, and right_on
how
(left, right, inner, outer) {{@}}suffixes
df1.merge(df2)
merge_ordered()
method:
on
, left_on
, and right_on
how
(left, right, inner, outer)suffixes
pd.merge_ordered(df1, df2)
Table Name: aapl
date close
0 2007-02-01 12.087143
1 2007-03-01 13.272857
2 2007-04-01 14.257143
3 2007-05-01 17.312857
4 2007-06-01 17.434286
Table Name: mcd
date close
0 2007-01-01 44.349998
1 2007-02-01 43.689999
2 2007-03-01 45.049999
3 2007-04-01 48.279999
4 2007-05-01 50.549999
import pandas as pd
pd.merge_ordered(aapl, mcd, on='date', suffixes=('_aapl','_mcd'))
date close_aapl close_mcd
0 2007-01-01 NaN 44.349998
1 2007-02-01 12.087143 43.689999
2 2007-03-01 13.272857 45.049999
3 2007-04-01 14.257143 48.279999
4 2007-05-01 17.312857 50.549999
5 2007-06-01 17.434286 NaN
pd.merge_ordered(aapl, mcd, on='date',
suffixes=('_aapl','_mcd'),
fill_method='ffill')
date close_aapl close_mcd
0 2007-01-01 NaN 44.349998
1 2007-02-01 12.087143 43.689999
2 2007-03-01 13.272857 45.049999
3 2007-04-01 14.257143 48.279999
4 2007-05-01 17.312857 50.549999
5 2007-06-01 17.434286 50.549999
pd.merge_ordered(aapl, mcd, on='date',
suffixes=('_aapl','_mcd'))
date close_aapl close_mcd
0 2007-01-01 NaN 44.349998
1 2007-02-01 12.087143 43.689999
2 2007-03-01 13.272857 45.049999
3 2007-04-01 14.257143 48.279999
4 2007-05-01 17.312857 50.549999
5 2007-06-01 17.434286 NaN
Joining Data with pandas