I saw many post talk about use reduce to merge multiple dataframe. I try below code, it works with three dataframes, if it greater than three, tan, error reported:
pandas.errors.MergeError: Passing 'suffixes' which cause duplicate columns {'value_x'} is not allowed.
code:
import pandas as pd
from functools import reduce
data1 = [
['A',1],
['B',2],
['C',3]]
data2 = [
['A',4],
['B',5],
['C',6]]
data3 = [
['A',7],
['B',8],
['C',9]]
data4 = [
['A',10],
['B',11],
['C',12]]
dfs = []
for data in [data1, data2, data3, data4]:
df = pd.DataFrame(data, columns=['name', 'value'])
dfs.append(df)
dfm = reduce(
lambda left, right: pd.merge(
left, right,
on=['name'],
how='outer',
suffixes=('_x', '_y')),
dfs)
print(dfm)
What's proper way to fix this error?