To use Python Pandas to merge multiple dataframes, we can call reduce
and merge
.
For instance, we write
import pandas as pd
from functools import reduce
df1 = pd.read_table('file1.csv', sep=',')
df2 = pd.read_table('file2.csv', sep=',')
df3 = pd.read_table('file3.csv', sep=',')
df_merged = reduce(lambda left,right: pd.merge(left,right,on=['DATE'],
how='outer'), data_frames)
to create 3 data frames from read_table
.
And then we call reduce
with a lambda to call pd.merge
with the data frames left
and right
to merge by the DATE
column values.
And we set how
to 'outer'
to do an outer join.