'How to assign a value to a cell in dataframe A based on a value in dataframe B, conditional on values of two other columns in B?

I'm really amateur-level with both python and pandas, but I'm trying to solve an issue for work that's stumping me.

I have two dataframes, let's call them dfA and dfB:

dfA:

     project_id    Category     Initiative

     10              
     20
     30
     40

dfB:

     project_id     field_id     value  
     10             100          lorem
     10             200          lorem1
     10             300          lorem2

     20             200          ipsum
     20             300          ipsum1
     20             500          ipsum2

Let's say I know "Category" from dfA correlates to field_id "100" from dfB, and "Initiative" correlates to field_id "200".

I need to look through dfB and for a given project_id/field_id combination, take the corresponding value in the "value" column and place it in the correct cell in dfA.

The result would look like this:

dfA:

     project_id    Category     Initiative

     10            lorem        lorem1
     20                         ipsum
     30
     40

Bonus difficulty: not every project in dfA exists in dfB, and not every field_id is used in every project_id.

I hope I've explained this well enough; I feel like there must be a relatively simple way to handle this that I'm missing.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source