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

Time:06-30

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

CodePudding user response:

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")
  • Related