Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较Python中的两个表_Python_Pandas - Fatal编程技术网

比较Python中的两个表

比较Python中的两个表,python,pandas,Python,Pandas,例如,我在Pandas中有两个数据帧: 首先是: id brand model 1 BMW E30 7 Mercedes W222 5 BMW E46 3 Mercedes W201 第二种是: id mpg weight 1 15 5000 3 12 4000 7 10 3000 5 20 4500 因此,我需要将带有mpg和权重的第二列放入带有ID检查的第一个数据帧中: id brand model mpg weight 1 BMW E30 15 5000 7 Mercedes W2

例如,我在Pandas中有两个数据帧:

首先是:

id brand model

1 BMW E30
7 Mercedes W222
5 BMW E46
3 Mercedes W201
第二种是:

id mpg weight

1 15 5000
3 12 4000
7 10 3000
5 20 4500
因此,我需要将带有mpg和权重的第二列放入带有ID检查的第一个数据帧中:

id brand model mpg weight
1 BMW E30 15 5000
7 Mercedes W222 10 3000
5 BMW E46 20 4500
3 Mercedes W201 12 4000
我该怎么做? 谢谢大家!

简单合并df1.mergedf2,how='left'
merged = df1.merge(df2, on=['id'])