x89
x89

Reputation: 3490

randomly assign a string to all rows in a column

I have a dataframe df['type']where I want to randomly assign one of the two string values: hello, bye to all rows in this column. How can I do so?

Upvotes: 1

Views: 154

Answers (2)

jezrael
jezrael

Reputation: 863701

Use numpy.random.choice with length of DataFrame:

df['type'] = np.random.choice(['hello','bye'], size=len(df))

For boolean:

df['type'] = np.random.choice([True, False], size=len(df))

Or:

df['type'] = np.random.choice([1, 0], size=len(df), dtype=bool)

Upvotes: 2

MrCont
MrCont

Reputation: 71

use the module random

random.choice(["hello","bye"])

Upvotes: 1

Related Questions