1

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
4
  • 3
    merging by index is just a concat pc.concat([df1,df2,df3], axis=1) Commented Jul 28, 2021 at 8:39
  • @Laz, In fact, I don't know where is the problem with your code line? What is the output of your code? Commented Jul 28, 2021 at 9:00
  • ah sorry, i see the problem here, i put a simple example but actually i work with a large data and each dataframe have 1 or 2 same columns name but with different values and i still want to merge it by index. Commented Jul 28, 2021 at 10:06
  • i already edited my problem Commented Jul 28, 2021 at 10:06

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.