在多列上左连接 [英] Left join on multiple columns

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

问题描述

我习惯将dplyr与R结合使用,

I'm used to using dplyr with R where I would do something like

library(dplyr)
mtcars2=mtcars
mtcars3 = mtcars %>% left_join(mtcars2[,c("mpg","vs","hp")], by =c("mpg",'hp') )

# what this does is I do a left join with multiple columns and then bring over only *1* additional column.  This means that mtcars3 only has one additional field - a duplicated 'vs'

我不知道如何使用pd.merge来做同样的事情. 我想由两列联接,然后带出 only 第三列-联接表中除合理的条件之外,不是联接表中的所有列

I can't figure out how to use pd.merge to do the same thing. I would want to join by two columns and then bring over only the 3rd column - not every column in the joined table except for the join-bys if that makes sense

import pandas as pd
mtcars = pd.read_csv('mtcars.csv')
mtcars2=mtcars

mtcars3  = pd.merge(mtcars, mtcars2['vs','hp','mpg'],how='left', on = ['mpg','hp'])

推荐答案

IIUC,您可以通过添加[]并省略mtcars2来使用子集-您可以再次使用mtcars:

IIUC you can use subset by adding [] and omit mtcars2 - you can use mtcars again:

import pandas as pd
mtcars = pd.read_csv('mtcars.csv')
mtcars3  = pd.merge(mtcars, mtcars[['vs','hp','mpg']], how='left', on = ['mpg','hp'])

示例:

import pandas as pd

mtcars = pd.DataFrame({'vs':[1,2,3],
                       'hp':[1,1,1],
                       'mpg':[7,7,9],
                       'aaa':[1,3,5]})

print (mtcars)
   aaa  hp  mpg  vs
0    1   1    7   1
1    3   1    7   2
2    5   1    9   3

mtcars3  = pd.merge(mtcars, mtcars[['vs','hp','mpg']], how='left', on = ['mpg','hp'])
print (mtcars3)
   aaa  hp  mpg  vs_x  vs_y
0    1   1    7     1     1
1    1   1    7     1     2
2    3   1    7     2     1
3    3   1    7     2     2
4    5   1    9     3     3

这篇关于在多列上左连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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