1

I have 4 dataframes with different names but they are equivalent:

diff_columns = ["Identificador de Producto", "Produtto ID", "Product ID", "P. ID"]
result = dfs[0]
for n, df in enumerate(dfs):
    result = pd.merge(result, df, left_on=diff_columns[i-1], right_on=diff_columns[i], how="outer")

How can I merge them dynamically on outer join?

There is an initial discussion on this topic but no clear way on this problem with different column names:

pandas three-way joining multiple dataframes on columns

Please also check the code given at the end:

from functools import reduce 

dfs_with_suffixes = [(df2,suffix2), (df3,suffix3), 
                     (df4,suffix4)]

merge_one = lambda x,y,sfx:pd.merge(x,y,on=['col1','col2'..], suffixes=sfx)

merged = reduce(lambda left,right:merge_one(left,*right), dfs_with_suffixes, df1)

This is from @Siddhant Tandon.

I would appreciate to find a solution for this problem.

Thank you.

matt.aurelio
  • 381
  • 2
  • 9

0 Answers0