x89
x89

Reputation: 3450

assign random values to all rows based on another column

I want to assign random numerical values ranging from 600-1200 to my column df['size'] but based on another column. So if the value of the same row in df['type']is yes, the value of df['size'] should be in a range from 600-1200 but if the value of df['type']is "no", then the value of df['size'] should be a random value between 1500-2000.

df['size'] = np.random.randint(600,1200, size=len(df))

Upvotes: 0

Views: 43

Answers (1)

jezrael
jezrael

Reputation: 862406

Use numpy.where for select if matched yes or not:

a = np.random.randint(600,1200, size=len(df))
b = np.random.randint(1500,2000, size=len(df))
df['size'] = np.where(df['type'].eq('yes'), a, b)

Upvotes: 2

Related Questions