2

I have two data frames as following

 Data Set A ID type msg 1 High Lets do 2 Low whats it 3 Medium thats it Data Set B ID Accounttype 2 Facebook 3 Linkedin 

How can I get an updated table with help of join in pandas, it should look like an

Updated DatasetA ID Account type msg 1 High Lets do 2 Facebook Low whats it 3 Linkedin Medium thats it 

I can easily do it in SQL with Update and inner join, how to perform it in pandas, I tried to do it, but most of the operations for append/ merge. Any help will be appreciated

7
  • Can you post what you tried as it maybe a simple fix, I think that A.merge(B, how='left') should work Commented Jul 4, 2016 at 15:45
  • A.update(other, join='inner') Commented Jul 4, 2016 at 15:57
  • so does A.merge(other, how='left') work? Commented Jul 4, 2016 at 15:58
  • No, its not working.. I want that single column Commented Jul 4, 2016 at 16:27
  • So, in reality, I have 100 columns in the right table, and I want just 1 column after the merge, just Account, that's again 1 more complexity Commented Jul 4, 2016 at 16:59

2 Answers 2

8

Try this:

df4: # ID type msg # 0 1 High Letsdo # 1 2 Low whatsit # 2 3 Medium thatsit 
df3: # ID Accounttype xxx # 0 2 Facebook 24 # 1 3 Linkedin 44 
df4.merge(df3[['ID', 'Accounttype']], how='left').fillna("") # ID type msg Accounttype # 0 1 High Letsdo # 1 2 Low whatsit Facebook # 2 3 Medium thatsit Linkedin 
Sign up to request clarification or add additional context in comments.

1 Comment

Makes complete Sense, Thanks Merlin
2

Seems there is no direct way to do it, so following is suggested

 a=b.merge(account,how='left',on='ID') 

create a list of columns you want in final data set

 list=['ID','Account','type','msg'] final=a[[col for col in list if col in b.columns]] 

It will give you only desired columns after the left join

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.