Richard Magowan
Richard Magowan

Reputation: 3

Python Pandas : joining selective columns from 1 data frame and add to another

I want to take 1 column from dataframe 'dfGS' and add it to dataframe 'df3'

If I just join the full dfGS to DF3 it works fine, but when I try to specify only 1 column to join I get : KeyError:'Ticker'**

'''df3=pd.merge( df3,dfGS['Shares to Trade'],how="inner",on='Ticker')'''

Ticker is the correct reference column in both df's , so not sure where I am going wrong?

error df3

dfGS

Upvotes: 0

Views: 29

Answers (1)

Deepansh Arora
Deepansh Arora

Reputation: 742

I would do it like this:

df3 = df3.merge(dfGS[['Shares to Trade',"Ticker"]], how="inner", left_on="Ticker", right_on="Ticker")

OR

df3 = df3.merge(dfGS[['Shares to Trade',"Ticker"]], how="inner", on="Ticker")

Upvotes: 1

Related Questions