Pandas:在选定列上加入数据框

Pandas: Join dataframes on selected columns

我有两个数据框如下

 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

如何在加入 pandas 的帮助下获得更新的 table,它应该看起来像 一个

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中做到,我试过了,但是大部分操作都是append/merge。任何帮助将不胜感激

似乎没有直接的方法,所以建议如下

 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]]

它只会在左连接后给你想要的列

试试这个:

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