Categories
Python Answers

How to use Python Pandas to merge multiple dataframes?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *