Menu Close

How to do three-way joining multiple dataframes on columns with Python Pandas?

To do three-way joining multiple dataframes on columns with Python Pandas, we call the reduce method.

For instance, we write

import pandas as pd
from functools import reduce


dfs = [df0, df1, df2, dfN]
df_final = reduce(lambda left,right: pd.merge(left,right,on='name'), dfs)

to call reduce with a lanbda that merges 2 data frames in the dfs list with the pd.merge method.

We merge them on the name column values.

And we set the initial value of df_final to dfs.

Posted in Python, Python Answers