pandas 加入具有不同名称的列 [英] Pandas join on columns with different names

查看:30
本文介绍了 pandas 加入具有不同名称的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数据框,我想对其执行一些 sql 操作.不幸的是,就像我正在处理的数据一样,拼写通常不同.

I have two different data frames that I want to perform some sql operations on. Unfortunately, as is the case with the data I'm working with, the spelling is often different.

请看下面的例子,我认为语法看起来像 userid 属于 df1 而 username 属于 df2.有人帮我吗?

See the below as an example with what I thought the syntax would look like where userid belongs to df1 and username belongs to df2. Anyone help me out?

 # not working - I assume some syntax issue?
pd.merge(df1, df2, on = [['userid'=='username', 'column1']], how = 'left')

推荐答案

名称不同时,使用xxx_on参数代替on=:

When the names are different, use the xxx_on parameters instead of on=:

pd.merge(df1, df2, left_on=  ['userid', 'column1'],
                   right_on= ['username', 'column1'], 
                   how = 'left')

这篇关于 pandas 加入具有不同名称的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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