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.

Junkrat
  • 2,754
  • 3
  • 19
  • 39
  • 2
    Does this answer your question? [How to create a DataFrame of random integers with Pandas?](https://stackoverflow.com/questions/32752292/how-to-create-a-dataframe-of-random-integers-with-pandas) – Junkrat Oct 11 '20 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') – Taylars Rikhotso Oct 11 '20 at 23:34

1 Answers1

0

Use apply function:

df['new_col'] = df.apply(lambda s: random.randint(15,33),axis=1)
Mehdi Golzadeh
  • 2,409
  • 1
  • 14
  • 26