Pandas:在不同名称的字段上加入DataFrames?

时间:2020-12-30 22:59:24

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

根据此文档,我只能在具有相同名称的字段之间进行连接。

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

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

The equivalent in SQL would be:

SQL中的等价物是:

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

1 个解决方案

#1


48  

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:

我认为你想要的是使用合并。传入left_on和right_on的关键字参数,告诉Pandas每个DataFrame中的哪些列用作键:

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

The documentation describes this in more detail on this page.

该文档在此页面上更详细地描述了这一点。

#1


48  

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:

我认为你想要的是使用合并。传入left_on和right_on的关键字参数,告诉Pandas每个DataFrame中的哪些列用作键:

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

The documentation describes this in more detail on this page.

该文档在此页面上更详细地描述了这一点。