Python 熊猫:组合不同大小的数据帧

Python 熊猫:组合不同大小的数据帧,python,pandas,Python,Pandas,我有两个数据帧: df1具有白色产品的ID和计数 product_id, count_white 12345,4 23456,7 34567,1 df2拥有所有产品的ID和计数 product_id,total_count 0009878,14 7862345,20 12345,10 456346,40 23456,30 0987352,10 34567,90 df2比df1有更多的产品。我需要在df2中搜索df1中的产品,并将total_count列添加到df1中: product_id,

我有两个数据帧:

df1具有白色产品的ID和计数

product_id, count_white
12345,4
23456,7
34567,1
df2拥有所有产品的ID和计数

product_id,total_count
0009878,14
7862345,20
12345,10
456346,40
23456,30
0987352,10
34567,90
df2比df1有更多的产品。我需要在df2中搜索df1中的产品,并将total_count列添加到df1中:

product_id,count_white,total_count
12345,4,10
23456,7,30
34567,1,90
我可以进行左合并,但最终会得到一个巨大的文件。有没有办法使用merge将特定行从df2添加到df1?

只需在“product\u id”列上执行左键:

In [12]:

df.merge(df1, on='product_id', how='left')
Out[12]:
   product_id  count_white  total_count
0       12345            4           10
1       23456            7           30
2       34567            1           90
执行左连接/合并: 数据帧是:

左连接:

df1=df1.merge(df2, on='product_id', how='left') 
输出如下所示:


不清楚为什么您认为左合并会产生一个巨大的文件,通过对产品id执行左合并,您表示您只对产品id列中的匹配感兴趣。我完全弄乱了数据帧,并在错误的数据帧上进行了合并。谢谢