darkpool
darkpool

Reputation: 14661

Adding dataframe columns onto another dataframe

I have the following two dataframes:

df1:

Symbol, Open, High, Low, Close
abc,    123,  676,  100, 343

df2:

Symbol, Target1, Target2
abc,    654,     565

I am trying to combine these two dataframes based on symbol, ie: Target1/Target2 must be added as new columns:

Symbol, Open, High, Low, Close, Target1, Target2
abc,    123,  676,  100, 343,   654,     565

I have tried a some join/merge ideas but can't seem to get it to work.

Please can someone advise.

Upvotes: 2

Views: 99

Answers (1)

EdChum
EdChum

Reputation: 394319

Just concat them and pass param axis=1:

In [7]:

pd.concat([df,df1], axis=1)
Out[7]:
  Symbol  Open  High  Low  Close Symbol  Target1  Target2
0    abc   123   676  100    343    abc      654      565

Or merge on 'Symbol' column:

In [8]:

df.merge(df1, on='Symbol')
Out[8]:
  Symbol  Open  High  Low  Close  Target1  Target2
0    abc   123   676  100    343      654      565

Upvotes: 3

Related Questions