Laz
Laz

Reputation: 29

Merge multiple dataframes by index

i have a list of data frames

df_list = [df1, df2, df3]

for example:

>> df1               >> df2                   >> df3
>>     a  b          >>      b  d             >>       e  b
    0  1  2               0  3  4                   0  5  6
    1  9  8               1  7  6                   1  5  4

each data frame have a same column name and i want to merge all of them by index. i tried

df_merge = pd.concat(df_list, axis=1)

but it doesnt have the expected output.

output

>> df_merge
>>    a    b    b    d    e    b
   0  1    6    6    4    5    6
   1  9    4    4    6    5    4

expected output:

>> df_merge
>>    a    b    b    d    e    b
   0  1    2    3    4    5    6
   1  9    8    7    6    5    4

Upvotes: 1

Views: 1549

Answers (1)

Corralien
Corralien

Reputation: 120409

Use pd.concat as suggested by @pythonic833:

>>> pd.concat(df_list, axis='columns')
   a  b  c  d  e  f
0  1  2  3  4  5  6
1  9  8  7  6  5  4

Please refer to the well explained documentation to Merge, join, concatenate and compare

Upvotes: 2

Related Questions