Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: combine data frames of different sizes

Tags:

python

pandas

I have 2 data frames:

df1 has ID and count of white products

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

df2 has IDs 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 search df2 for products that are in df1 and add total_count column 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 end up with a huge file. Is there any way to add specific rows from df2 to df1 using merge?

like image 957
Anastasia Avatar asked Dec 18 '14 21:12

Anastasia


1 Answers

Just perform a left merge on '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
like image 51
EdChum Avatar answered Oct 17 '22 07:10

EdChum