Skip to content
Advertisement

Pandas return separate column value in current index if two separate columns match

Say I have the following data frame:

JavaScript

I have been trying to:

  1. Loop through Column A to find a matching value in Column B
  2. If there is a match in Column B I want to grab the value in Column C for the current index and create a Column D with that value.
  3. Given the example data frame above, below would be the solution I’m trying to achieve.
JavaScript

I’ve seen lots of answers for excel utilizing match and index, but I literally can’t find anything to help me solve this problem. Any help would be appreciated.

Advertisement

Answer

Use map with set_index:

JavaScript

Output:

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