0

I have this Pandas Dataframe

MasterID  product_id  packing_id   Product Name 
1            1                      Apple
1                       1A          
1           1B
1           1C
2                       2A          Banana
3            3                      Orange
3                       3A
4            4                      kiwi
5                       5A          grape
5                       5B

I would like like to return a "product family" column. In excel, I would do a Vlookup on the master ID and return the product name in my column.

MasterID  product_id  packing_id   Product Name  Product Family
1            1                      Apple          Apple
1                       1A                         Apple
1           1B                                     Apple
1           1C                                     Apple
2                       2A          Banana         Banana
3            3                      Orange         Orange 
3                       3A                         Orange
4            4                      kiwi           Kiwi
5                       5A          grape          grape
5                       5B                         grape

How I do this in Python?

Simon GIS
  • 1,045
  • 2
  • 18
  • 37

1 Answers1

0

From @anky

df['Product Family'] = df.groupby('MasterID')['Product Name'].transform('first')

Simon GIS
  • 1,045
  • 2
  • 18
  • 37