通过列名称连接 pandas 数据框 [英] Joining pandas dataframes by column names
问题描述
我有两个具有以下列名称的数据框:
I have two dataframes with the following column names:
frame_1:
event_id, date, time, county_ID
frame_2:
countyid, state
我想通过在county_ID = countyid
上连接(左)来获得包含以下列的数据框:
I would like to get a dataframe with the following columns by joining (left) on county_ID = countyid
:
joined_dataframe
event_id, date, time, county, state
如果我要连接的列不是索引,我无法弄清楚该怎么做.最简单的方法是什么?谢谢!
I cannot figure out how to do it if the columns on which I want to join are not the index. What's the easiest way? Thanks!
推荐答案
您可以如下使用left_on和right_on选项:
you can use the left_on and right_on options as follows:
pd.merge(frame_1, frame_2, left_on='county_ID', right_on='countyid')
从问题中我不能确定您是否只想合并密钥是否位于左侧数据框中.如果是这种情况,则以下将执行此操作(以上内容实际上会进行多对多合并)
I was not sure from the question if you only wanted to merge if the key was in the left hand dataframe. If that is the case then the following will do that (the above will in effect do a many to many merge)
pd.merge(frame_1, frame_2, how='left', left_on='county_ID', right_on='countyid')
这篇关于通过列名称连接 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!