Skip to content
Advertisement

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

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

Advertisement

Answer

You can use np.random.choice for this:

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