通过列值 pandas 联接数据框 [英] Join dataframes by column values pandas

查看:73
本文介绍了通过列值 pandas 联接数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个来自不同数据库的数据帧df1df2.数据框中的每个项目都由id标识.

I have two data frames df1 and df2 taken from different databases. Each item in the dataframes is identified by an id.

df1 = pd.DataFrame({'id':[10,20,30,50,100,110],'cost':[100,0,300,570,400,140]})

df2 = pd.DataFrame({'id':[10,23,30,58,100,110],'name':['a','b','j','d','k','g']})

在两个数据框中都有一些常见的产品,在这种情况下,这些产品的ID为:10,30,100,110. 我想将此信息合并到一个数据框中,如下所示:

there are some common products in both dataframes, in this case those with the ids: 10,30,100,110. I want to merge this information in one single dataframe, as this one:

df3 = pd.DataFrame({'id':[10,30,100,110],'name':['a','j','k','g'],'cost':[100,300,400,140]})

我试图用字典和嵌套循环来实现它,但是我处理了大量的数据,而且这样做花了很长时间.

I was trying to do it with dictionaries and nested loops but I handling a rather big amount of data and it just take to long to do it that way.

推荐答案

我认为您可以使用 查看全文

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