0

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))

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.