0

I want to add a new column to the dataframe that will create a unique random number between 15 and 33.

i have tried this:

import random
df[new_col] = random.randint(15,33)

The problem with this code is that it creates one random number and distribute in all the columns, whereas I want each row to have its unique random number.

I think I should use a for loop but kinda don't know how to start.

2
  • 2
    Does this answer your question? How to create a DataFrame of random integers with Pandas? Commented Oct 11, 2020 at 16:21
  • @Junkrat, This is not working. I implemented it in my code and it throws an a typ error. import random import numpy as np ride_sharing['tyre_size_2'] = pd.DataFrame(np.random.randint(15,33, size=(25760,1)), columns='tyre_size_2') Commented Oct 11, 2020 at 23:34

1 Answer 1

0

Use apply function:

df['new_col'] = df.apply(lambda s: random.randint(15,33),axis=1)
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.