pandas :在选定列上联接数据框 [英] Pandas: Join dataframes on selected columns

查看:55
本文介绍了 pandas :在选定列上联接数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据帧,如下所示

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

我可以轻松地在SQL中使用Update和内部联接来完成它,如何在熊猫中执行它,我试图做到这一点,但是大多数操作都是针对append/merge的.任何帮助将不胜感激

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

推荐答案

尝试一下:

 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

这篇关于 pandas :在选定列上联接数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆