'pandas - merge multiple dataframe column name

I would like to merge multiple dataframe. I got below example work but the column name a little bit confuse:

import pandas as pd

def mergedf(dfs, countfiles,oncolumn,i=0):
    if i == (countfiles - 1): # it gets to the second to last and merges it with the last
        return dfs[i]

    dfm = dfs[i].merge(mergedf(dfs, countfiles,oncolumn,i=i+1), on=oncolumn)
    return dfm

df1 = pd.DataFrame([["x", 1],["y", 2]], columns=["A", "B"])
df2 = pd.DataFrame([["x", 3],["y", 4]], columns=["A", "B"])
df3 = pd.DataFrame([["x", 5],["y", 6]], columns=["A", "B"])
df4 = pd.DataFrame([["x", 7],["y", 8]], columns=["A", "B"])
df5 = pd.DataFrame([["x", 9],["y", 10]], columns=["A", "B"])

print(df1)

dfs = [df1,df2,df3,df4,df5]
df = mergedf(dfs, len(dfs),'A')

print(df)

Current output:

   A  B
0  x  1
1  y  2

   A  B  B_x  B_y  B_x  B_y
0  x  1    3    5    7    9
1  y  2    4    6    8   10

The colum name is (A,B,B_x,B_y,B_x,B_y),some column name is repeated. I would like the result column name as (A,B0,B1,B2,B3,B4),just as below:

   A  B0  B1   B2   B3   B4
0  x  1    3    5    7    9
1  y  2    4    6    8   10


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source