Ashim Sinha
Ashim Sinha

Reputation: 577

concat two dataframe using python

We have one dataframe like

-0.140447131    0.124802527 0.140780106
0.062166349    -0.121484447 -0.140675515
-0.002989106    0.13984927  0.004382326

and the other as

1
1
2

We need to concat both the dataframe like

-0.140447131    0.124802527 0.140780106  1
0.062166349    -0.121484447 -0.140675515 1
-0.002989106    0.13984927  0.004382326  2

Upvotes: 0

Views: 319

Answers (2)

Alexander
Alexander

Reputation: 109726

Use pd.concat and specify the axis equal to 1 (rows):

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

>>> df_new
          0         1         2  0
0 -0.140447  0.124803  0.140780  1
1  0.062166 -0.121484 -0.140676  2
2 -0.002989  0.139849  0.004382  3

Upvotes: 1

Zero
Zero

Reputation: 77027

Let's say your first dataframe is like

In [281]: df1
Out[281]:
          a         b         c
0 -0.140447  0.124803  0.140780
1  0.062166 -0.121484 -0.140676
2 -0.002989  0.139849  0.004382

And, the second like,

In [283]: df2
Out[283]:
   d
0  1
1  1
2  2

Then you could create new column for df1 using df2

In [284]: df1['d_new'] = df2['d']

In [285]: df1
Out[285]:
          a         b         c  d_new
0 -0.140447  0.124803  0.140780      1
1  0.062166 -0.121484 -0.140676      1
2 -0.002989  0.139849  0.004382      2

The assumption however being both dataframes have common index

Upvotes: 1

Related Questions