Open In App

Generating Random Integers in Pandas Dataframe

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas is the most popular Python library that is used for data analysis. It provides highly optimized performance with back-end source code that is purely written in C or Python. Here we will see how to generate random integers in the Pandas datagram. We will be using the numpy.random.randint() method to generate random integers.

Generating Random Integers in Pandas Dataframe

Here, are various ways to Generating Random Integers in Pandas Dataframe using different delimiters with Pandas in Python. here we are discussing some generally used methods for Generating Random Integers in Pandas Dataframe using different delimiters with Pandas in Python.

  • Using Pandas and NumPy in Python
  • Using random.randint() Method
  • Using Random Integers
  • Using Random Integers in Ascending Order

Generate Random Integers Using Pandas and NumPy in Python

In this example, we are using different delimiters with pandas in Python for generate random integer as in below Python code uses pandas and Numpy to create a DataFrame with 11 random integers between 5 and 35, and then prints the result.

Python3
# importing pandas and numpy libraries
import numpy as np
import pandas as pd

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

# displaying random integers in data frame
print(df)

Output :

  random_numbers
0 21
1 8
2 7
3 10
4 34
5 20
6 20
7 11
8 5
9 17
10 10

Generate Random Integers Using random.randint() Method

In this example , below Python’s pandas and numpy libraries to create a dataframe with 7 random integers, sorts them in ascending order, and displays the result.

Python3
# importing pandas and numpy libraries
import numpy as np
import pandas as pd

# generating 7 random integers from 5 to 35
data = np.random.randint(5, 35, size = 7)
df = pd.DataFrame(data, columns = ['integers'])

# displaying random integers in data frame
print("Before Sorting :")
print(df)

df.sort_values("integers", axis = 0, ascending = True,
                inplace = True, na_position ='last')

print("After Sorting :")
print(df)

Output :

Before Sorting :
integers
0 17
1 21
2 28
3 12
4 29
5 31
6 7
After Sorting :
integers
6 7
3 12
0 17
1 21
2 28
4 29
5 31
 

Generating Random Integers Using Random Integers

In this example , below Python’s pandas and numpy libraries to create a DataFrame (tabular data structure) with 12 rows and 3 columns, filled with random integers between 5 and 40.

Python3
# importing pandas and numpy libraries
import numpy as np
import pandas as pd

data = np.random.randint(5, 40, size=(12, 3))
df = pd.DataFrame(data, columns=['random_no_1',
                                 'random_no_2',
                                 'random_no_3'])

# displaying random integers in the dataframe
print(df)

Output :

  random_no_1  random_no_2  random_no_3
0 12 32 38
1 14 18 7
2 15 15 18
3 7 33 18
4 14 30 15
5 26 9 19
6 28 34 10
7 15 23 31
8 31 12 37
9 38 28 25
10 8 39 22
11 20 6 20 

Generating Random Integers Using Random Integers in Ascending Order

In this example , below Python code uses pandas and numpy to create a 6×2 DataFrame with random integers, sorts it based on two columns in ascending order, and prints the DataFrame before and after sorting.

Python3
# importing pandas and numpy libraries
import numpy as np
import pandas as pd

# generating 6x2 i.e 12 random integers
# from 5 to 40
data = np.random.randint(5, 40, size = (6, 2))
df = pd.DataFrame(data, columns = ['random_col_1', 'random_col_2'])

# displaying random integers in data frame
print("Before Sorting :")
print(df)

df.sort_values(['random_col_1', 'random_col_2'], axis = 0,
            ascending = [True, True], inplace = True)

print("After Sorting :")
print(df)

Output :

Before Sorting :
random_col_1 random_col_2
0 22 37
1 8 21
2 8 12
3 24 18
4 23 34
5 10 11
After Sorting :
random_col_1 random_col_2
2 8 12
1 8 21
5 10 11
0 22 37
4 23 34
3 24 18 


Last Updated : 18 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads