Joining Data on Pandas: Dataframe Merge Examples

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Note that using pd.merge(left_df,right_df) can also be written as left_df.merge(right_df)

Simple join using pd.merge()

The default is an inner join. Use 'on'='left'|'right'|'outer' to change join types.

    left_on= <column_on_left_dataframe>, 
    right_on= <column_on_right_dataframe>)

Join on indices

If the columns you want to join on are Indices, use left_index and right_index:

    how= <'inner','left','right'>, 

Join on multiple columns


pd.merge vs dataframe.join


Approximate match join

I.e. join a row on left_df with a row on right_df where column column_right on right_df is the closest to column_left on left_df, according to a given distance function.


  • This looks very promising

  • Maybe I could use this to snap the dates to the nearest month and then do an exact match:

  • merge_closest

  • np.piecewise

  • IntervalIndex

Merge on closest date

TODO a particular case of the above?

Merge with date range

TODO a particular case of the above?

Merge by year/month/day of the week

TODO a particular case of the above?


Dialogue & Discussion