Skip to content
Advertisement

One to many mapping with a ChainMap Dictionary

I would like do a one to many mapping with the following list and mapping dictonary:

JavaScript

This is my dataframe:

JavaScript

When I do the mapping as follows:

JavaScript

The problem is that I want to do a one to many mapping and I don’t capture the relationship. The desired outcome would be something like this. Which creates a new line when there are multiple relationships.

JavaScript

Thank you for your support.

Advertisement

Answer

Here ChainMap can’t be used since it will not preserve all the duplicate keys. The solution is to create an intermediate dataframe from each pairs of (mapping, code) and then left merge that with the original dataframe

JavaScript

Result

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