How to Create a Pandas DataFrame of Random Integers
In this quick guide, we're going to create a Pandas DataFrame of random integers with arbitrary length.
import numpy as np
import pandas as pd
import string
string.ascii_lowercase
n = 5
m = 7
cols = string.ascii_lowercase[:m]
df = pd.DataFrame(np.random.randint(0, n,size=(n , m)), columns=list(cols))
df
Explanation:
- n - number of the rows
- m - number of the columns. The columns will be named with latin letters in lowercase
np.random.randint
- will be used to produce random integers in a range of n to m.
The produced DataFrame with random integer numbers is:
a | b | c | d | e | f | g | |
---|---|---|---|---|---|---|---|
0 | 2 | 3 | 1 | 0 | 3 | 2 | 1 |
1 | 1 | 4 | 3 | 3 | 3 | 0 | 4 |
2 | 4 | 2 | 3 | 2 | 2 | 2 | 3 |
3 | 3 | 1 | 1 | 3 | 2 | 2 | 4 |
4 | 3 | 0 | 0 | 3 | 0 | 3 | 2 |