Skip to content
Advertisement

Update pandas cell in one dataframe from looked up value in second dataframe

Update pandas cell in one Dataframe from looked up value in second Dataframe

I have a case where I need to update a cell in one Dataframe, ‘Stock‘, which holds records of stock on-hand, looking up its value in a second Dataframe, ‘Items‘, which is the table of all items. This is a simplified example of the Dataframes with relevant fields.

Stock

JavaScript

Items

JavaScript

The challenge

I have previously done this in PostgreSQL but would like to see if I can do all the processing in Pandas (I plan to link to the PostgreSQL table of items). If we look at the Stock table the item_no column should only have item numbers, see Items Dataframe (table), but sometimes the users put in the model_no instead of the item number. So in the Stock dataframe, row 2 incorrectly has the value MODEL_B.

What’s needed

What is needed to be done is to:

  1. get the value MODEL_B from the item_no column in the Stock dataframe
  2. find that in the model_no column of the Items dataframe
  3. then get the value from the item_no field of the Items dataframe
  4. use that value to replace the (incorrect) model number value in the item_no column of the Stock dataframe

It gets a little more challenging… a model may have more than one part number:

JavaScript

In this case the ‘highest’ part number, in this case 9H.333, needs to be used. In SQL I use the MAX() operator.

I would like to perform this using ‘set’ operations in pandas (not looping), similar to running a query in SQL. So this would mean (?) joining the two dataframes on the fields stock.item_no <-> items.model_no (?) – I’m not sure how to go about it hence the question marks.

Generate Dataframes

This code will generate the dataframes discussed above.

JavaScript

Advertisement

Answer

You can use:

JavaScript

updated stock:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement