How to fill a pandas dataframe column with one of two list values?

Question:

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.

sex = ['Male', 'Female']
def sex():
    for row in tips['sex']:
        sex[random.randint(0,1)]
tips['sex'] = sex()
Asked By: Exquisite_Poupon

||

Answers:

You can use np.random.choice for this:

import numpy as np
import pandas as pd
df = pd.DataFrame({'x': [1, 3, 4, 5, 7]})

df['sex'] = np.random.choice(['Male', 'Female'], size=len(df))
df

   x     sex
0  1    Male
1  3    Male
2  4    Male
3  5  Female
4  7    Male
Answered By: Psidom

(Far worse) Loop Methods:

sex_col = []
for i in range(len(df)):
    sex_col.append(sex[random.randint(0,1)])

df['sex'] = sex_col
for i in df.index:
    df.loc[i, 'sex'] = sex[random.randint(0,1)]
Answered By: BeRT2me
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.