Generate random number in pandas python

In order to generate random number in pandas python we need to use the randint() function. Let’s see how to

  • Generate random number to the column in pandas python with example

First let’s create a dataframe


import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona','Georgia','Newyork','Indiana','Florida'],
   'Score1':[4,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score1'])
print(df1)

df1 will be

Generate random number in pandas python 2

 

We generate random number using  randint() function with the size equal to the length of the dataframe and result is stored in a new column as shown below.

df1['Random_score'] = np.random.randint(0,1000,size=(len(df1),1))
print(df1)

Here we are generating random number between 1 and 1000 using randint() function. So the resultant dataframe will be

Generate random number in pandas python 1

 

p Generate random number in pandas python                                                                                                                n Generate random number in pandas python