Pandas: merge data frames of different sizes

I have 2 data frames:

df1 has id and number of white products

product_id, count_white
12345,4
23456,7
34567,1

df2 has identifiers and counts of all products

product_id,total_count
0009878,14
7862345,20
12345,10
456346,40
23456,30
0987352,10
34567,90

df2 has more products than df1. I need to find df2 for products located in df1 and add the column total_count to df1:

product_id,count_white,total_count
12345,4,10
23456,7,30
34567,1,90

I could do a left merge, but I would get a huge file. Is there a way to add specific rows from df2 to df1 using merge?

+4
source share
1 answer

Just do the left mergein the 'product_id' column:

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
+3
source

Source: https://habr.com/ru/post/1568230/


All Articles