Pandas: Combining Two DataFrames Horizontally


I have two Pandas DataFrames, each with different columns. I want to basically glue them together horizontally (they each have the same number of rows so this shouldn’t be an issue).

There must be a simple way of doing this but I’ve gone through the docs and concat isn’t what I’m looking for (I don’t think).

Any ideas?


Asked By: anon_swe



concat is indeed what you’re looking for, you just have to pass it a different value for the "axis" argument than the default. Code sample below:

import pandas as pd

df1 = pd.DataFrame({
    'A': [1,2,3,4,5],
    'B': [1,2,3,4,5]

df2 = pd.DataFrame({
    'C': [1,2,3,4,5],
    'D': [1,2,3,4,5]

df_concat = pd.concat([df1, df2], axis=1)


With the result being:

   A  B  C  D
0  1  1  1  1
1  2  2  2  2
2  3  3  3  3
3  4  4  4  4
4  5  5  5  5
Answered By: nslamberth
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.