How to create a DataFrame of random integers with Pandas?

By using the random integers, we have to create a Pandas DataFrame.
Submitted by Pranit Sharma, on June 18, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data.

To create a DataFrame of random integers in Pandas, we will use the "random" library of python.

The "random" library in python is useful for generating random values within the provided range. "randint" component of the random library is used to generate random integers between the specified range.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code to create a DataFrame of random integers

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Generating random integers
data = np.random.randint(10, 50, size=15)

# Creating a DataFrame
df = pd.DataFrame(data,columns=['random_integers'])

# Display DataFrame with NaN values
print("Created DataFrames:\n",df,"\n")


Example: Create a DataFrame of random integers

Python Pandas Programs »


Comments and Discussions!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.