I am trying to add a ‘sex’ column to an existing ‘tips’ dataframe. There are 244 rows that need to be filled randomly with either ‘Male’ or ‘Female’. I have tried using a for loop to iterate through each row and assign either list option, but I can’t quite get it right.
JavaScript
x
6
1
sex = ['Male', 'Female']
2
def sex():
3
for row in tips['sex']:
4
sex[random.randint(0,1)]
5
tips['sex'] = sex()
6
Advertisement
Answer
You can use np.random.choice
for this:
JavaScript
1
14
14
1
import numpy as np
2
import pandas as pd
3
df = pd.DataFrame({'x': [1, 3, 4, 5, 7]})
4
5
df['sex'] = np.random.choice(['Male', 'Female'], size=len(df))
6
df
7
8
x sex
9
0 1 Male
10
1 3 Male
11
2 4 Male
12
3 5 Female
13
4 7 Male
14