merge 2 dataframes based on date in python

Assuming you have two dataframes df1 and df2 which contain a column with dates that you want to merge on:

main.py
import pandas as pd

merged_df = pd.merge(df1, df2, on='date_column')
70 chars
4 lines

Make sure that the date format is the same in both dataframes. By default, pd.merge() does an inner join on the two dataframes. You can adjust this behavior by specifying the how parameter, which can take one of the following values: 'left', 'right', 'outer', 'inner'.

For example, to perform a left join:

main.py
merged_df = pd.merge(df1, df2, on='date_column', how='left')
61 chars
2 lines

related categories

gistlibby LogSnag