Appending a dataframe to the right of another one with the same columns

Viewed 8796

I have two different dataframes with the same column names:

eg.

     0   1   2
0   10  13  17
1   14  21  34
2   68  32  12

     0   1   2 
0   45  56  32
1    9  22  86
2   55  64  19

I would like to append the second frame to the right of the first one while continuing the column names from the first frame. The output would look like this:

    0   1   2   3   4   5
0  10  13  17  45  56  32
1  14  21  34   9  22  86
2  68  32  12  55  64  19

What is the most efficient way of doing this? Thanks.

2 Answers

Why not join:

>>> df=df.join(df_,lsuffix='_')
>>> df.columns=range(len(df.columns))
>>> df
    0   1   2   3   4   5
0  10  13  17  45  56  32
1  14  21  34   9  22  86
2  68  32  12  55  64  19
>>> 

join is your friend, i use lsuffix (could be rsuffix too) to ignore error for saying duplicate columns.

Related