Generating Random Integers in Pandas Dataframe
Last Updated :
18 Mar, 2024
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 6x2 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
Similar Reads
Convert Floats to Integers in a Pandas DataFrame Let us see how to convert float to integer in a Pandas DataFrame. We will be using the astype() method to do this. It can also be done using the apply() method. Convert Floats to Integers in a Pandas DataFrameBelow are the ways by which we can convert floats to integers in a Pandas DataFrame: Using
3 min read
How to Convert Integers to Floats in Pandas DataFrame? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, etc. There are 2 methods to convert Integers to Floats: Method 1: Using DataFrame.astype() method Syntax :Â DataFrame.astype(dtype, co
4 min read
How to Convert Integers to Strings in Pandas DataFrame? In this article, we'll look at different methods to convert an integer into a string in a Pandas dataframe. In Pandas, there are different functions that we can use to achieve this task : map(str)astype(str)apply(str)applymap(str) Example 1 : In this example, we'll convert each value of a column of
3 min read
Get first N records in Pandas DataFrame When working with large datasets in Python using the Pandas library, it is often necessary to extract a specific number of records from a column to analyze or process the data, such as the first 10 values from a column. For instance, if you have a DataFrame df with column A, you can quickly get firs
5 min read
Insert row at given position in Pandas Dataframe Inserting a row in Pandas DataFrame is a very straight forward process and we have already discussed approaches in how insert rows at the start of the Dataframe. Now, let's discuss the ways in which we can insert a row at any position in the dataframe having integer based index.Solution #1 : There d
3 min read
How to Convert String to Integer in Pandas DataFrame? Let's see methods to convert string to an integer in Pandas DataFrame: Method 1: Use of Series.astype() method. Syntax: Series.astype(dtype, copy=True, errors=âraiseâ) Parameters: This method will take following parameters: dtype: Data type to convert the series into. (for example str, float, int).c
3 min read