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

查看:71
本文介绍了 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天全站免登陆