Home > Software design >  Generating M or F for a 100 data point Python
Generating M or F for a 100 data point Python

Time:07-10

Is there a way of generating a random sample of Male and Female for my dataframe in Python? Doesnt have to be 50:50 but random every time I ran it? Thanks.

CodePudding user response:

Initialise a RNG object and use its choice method:

from numpy.random import default_rng

rng = default_rng()
values = rng.choice(['M', 'F'], size=100)

CodePudding user response:

import random

sample = ['M' if random.randint(0, 1) == 1 else 'F' for _ in range(100)]

Output:

['M', 'M', 'M', 'M', 'M', 'F', 'M', 'M', 'M', 'F', 'M', 'M', 'M', 'M', 'M', 'M', 'F', 'F', 'M', 'M', 'M', 'F', 'M', 'M', 'M', 'F', 'F', 'M', 'F', 'F', 'F', 'M', 'M', 'M', 'M', 'F', 'M', 'M', 'F', 'F', 'M', 'F', 'M', 'F', 'F', 'M', 'M', 'F', 'M', 'M', 'M', 'F', 'F', 'M', 'M', 'M', 'M', 'F', 'M', 'F', 'M', 'M', 'F', 'F', 'F', 'M', 'F', 'M', 'F', 'F', 'F', 'F', 'M', 'F', 'F', 'F', 'M', 'F', 'F', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'F', 'M', 'F', 'M', 'M', 'F', 'M', 'M', 'M', 'M', 'M', 'M', 'M']
  • Related