0

I have a dataframe that outputs a Product Code:

Product Code Price
61725 $50
26753 $12
61725 $30
176534 $10

How do I create a new column called "Product Name" that outputs the correct Product Name based on Product code? I have a dictionary that looks like this:

Product_Mapping = {61725:'Apples', 26753:'Cherries',176534:'Peaches'}

I want my final df to look like this:

Product Code Price Product Name
61725 $50 Apples
26753 $12 Cherries
61725 $30 Apples
176534 $10 Peaches
Calzone
  • 3
  • 2

1 Answers1

0

Use map

Product_Mapping = {61725:'Apples', 26753:'Cherries',176534:'Peaches'}

df["Product Name"] = df["Product Code"].map(Product_Mapping)


print(df)

   Product Code Price Product Name
0         61725   $50       Apples
1         26753   $12     Cherries
2         61725   $30       Apples
3        176534   $10      Peaches
Ian Thompson
  • 2,914
  • 2
  • 18
  • 31