Pandas:在具有不同名称的字段上加入 DataFrames? [英] Pandas: join DataFrames on field with different names?

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

问题描述

根据本文档我可以仅在具有相同名称的字段之间进行连接.

According to this documentation I can only make a join between fields having the same name.

您知道是否可以在具有不同名称的字段上连接两个 DataFrame 吗?

Do you know if it's possible to join two DataFrames on a field having different names?

SQL 中的等价物是:

The equivalent in SQL would be:

SELECT *
FROM df1
LEFT OUTER JOIN df2
  ON df1.id_key = df2.fk_key

推荐答案

我认为使用 merge 可以实现您的需求.传入 left_onright_on 的关键字参数,以告诉 Pandas 将每个 DataFrame 中的哪些列用作键:

I think what you want is possible using merge. Pass in the keyword arguments for left_on and right_on to tell Pandas which column(s) from each DataFrame to use as keys:

pandas.merge(df1, df2, how='left', left_on=['id_key'], right_on=['fk_key'])

文档在此页面上对此进行了更详细的描述.

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

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