2

On first dataframe, I have some product SKU codes and their respectives unique IDs. In the other one I have the SKUs of several products, including some of the first dataframe. I want to get the unique product ids from df1 and assign them to df2, also completing the SKU numbers that don't match:

Example:

df1:                                df2:

sku     | unique_id                 sku     |
------- | ---------                 ------- | 
4fd5.88 | 1                         4fd5.88 | 
d545.99 | 2                         d545.99 | 
hy12.33 | 3                         h999.21 | 

Desired output:

df1:                                df2:

sku     | unique_id                 sku     | unique_id
------- | ---------                 ------- | ---------
4fd5.88 | 1                         4fd5.88 | 1
d545.99 | 2                         h999.21 | 3
hy12.33 | 3                         d545.99 | 2                        
samurai-py
  • 21
  • 3

1 Answers1

1
df2['unique_id']=df2['sku'].map(df.set_index(['sku'])['unique_id'])
df2
sku     unique_id
0   4fd5.88     1.0
1   d545.99     2.0
2   h999.21     NaN
Naveed
  • 11,495
  • 2
  • 14
  • 21