2016-07-04 124 views
0

我有两个数据帧加入dataframes如下熊猫:在选定列

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 

我怎样才能获得更新的表与大熊猫一起的帮助下,它应该看起来像 的

Updated DatasetA 

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

我可以很容易地在SQL中使用Update和inner join来完成它,如何在pandas中执行它,我试图做到这一点,但大多数操作是追加/合并。任何帮助将不胜感激

+0

你可以发布你试过的,因为它可能是一个简单的修复,我认为'A.merge(B,how ='left')'应该可以工作 – EdChum

+0

A.update(other,join ='inner') –

+0

so A.merge(其他,如何='左')'工作? – EdChum

回答

1

试试这个:

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 
+0

使完整感,感谢梅林 –

0

似乎没有直接的办法做到这一点,那么以下建议

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

创建在最后的数据要列的列表中设置

list=['ID','Account','type','msg'] 

final=a[[col for col in list if col in b.columns]] 

它将左边加入后只给你想要的列