Skip to content
Advertisement

creating random values but within another column restraints [python]

I have this type of dataset:

JavaScript

I want to have:

JavaScript

As you can see, if I just went and created random values for the Gender column, I will eventually have a problem: I might assign different gender names to the same person ID. If I had unique IDs, then, that wouldn’t have been a problem. But I want to create random value for gender, but within the constraint that they are assigned the same for the same ID. How to accomplish that in Python?

Advertisement

Answer

using random.choice and .replace:

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