2016-07-04 122 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]] 

它將左邊加入後只給你想要的列