How to Generate Random Integers in Pandas Dataframe

In this short guide, you’ll see how to generate random integers in Pandas DataFrame under:

  • Single DataFrame column
  • Multiple DataFrame columns

You’ll also see how to convert those integers to different data types, such as floats or strings.

Generate Random Integers under a Single DataFrame Column

Here is a template that you may use to generate random integers under a single DataFrame column:

import numpy as np
import pandas as pd

data = np.random.randint(lowest integer, highest integer, size=number of random integers)
df = pd.DataFrame(data, columns=['column name'])

print(df)

For example, let’s say that you want to generate random integers given the following information:

  • The lowest integer is 5 (inclusive)
  • The highest integer is 30 (exclusive)
  • The size is 10

You may then apply this code in Python:

import numpy as np
import pandas as pd

data = np.random.randint(5,30,size=10)
df = pd.DataFrame(data, columns=['random_numbers'])

print(df)

When you run the code, you’ll get 10 random integers (as specified by the size of 10):

How to Generate Random Integers in Pandas Dataframe

You may note that the lowest integer (e.g., 5 in the code above) may be included when generating the random integers, but the highest integer (e.g., 30 in the code above) will be excluded.

Generate Random Integers under Multiple DataFrame Columns

Here is a template to generate random integers under multiple DataFrame columns:

import pandas as pd

data = np.random.randint(lowest integer, highest integer, size=(number of random integers per column, number of columns))
df = pd.DataFrame(data, columns=['column name 1', 'column name 2', 'column name 3',...])

print(df)

For instance, you can apply the code below in order to create 3 columns with random integers:

import numpy as np
import pandas as pd

data = np.random.randint(5,30,size=(10,3))
df = pd.DataFrame(data, columns=['random_numbers_1', 'random_numbers_2', 'random_numbers_3'])

print(df)

And here is the result:

Generate Random Integers in Pandas Dataframe

Check the Data Type

You can check the data type in Pandas DataFrame by adding print(df.dtypes) at the bottom of the code:

import numpy as np
import pandas as pd

data = np.random.randint(5,30,size=(10,3))
df = pd.DataFrame(data, columns=['random_numbers_1', 'random_numbers_2', 'random_numbers_3'])

print(df)
print(df.dtypes)

As you may observe, the data type for each of the 3 columns is integer:

Data Type of integers

Convert the Data Type to Float

You can convert the integers to floats by applying astype(float) as follows:

import numpy as np
import pandas as pd

data = np.random.randint(5,30,size=(10,3))
df = pd.DataFrame(data, columns=['random_numbers_1', 'random_numbers_2', 'random_numbers_3']).astype(float)

print(df)
print(df.dtypes)

You’ll see that the data type for each of the 3 columns is now float:

How to Generate Random numbers in Pandas Dataframe

Convert the Data Type to String

Alternatively, you may convert the integers to strings using astype(str):

import numpy as np
import pandas as pd

data = np.random.randint(5,30,size=(10,3))
df = pd.DataFrame(data, columns=['random_numbers_1', 'random_numbers_2', 'random_numbers_3']).astype(str)

print(df)
print(df.dtypes)

You’ll now get ‘object’ which represents strings:

Strings

You may also want to check the numpy manual for further information about numpy.random.randint.