How to create a DataFrame of random integers with Pandas?
numpy.random.randint
accepts a third argument (size
) , in which you can specify the size of the output array. You can use this to create your DataFrame
-
df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
Here - np.random.randint(0,100,size=(100, 4))
- creates an output array of size (100,4)
with random integer elements between [0,100)
.
Demo -
import numpy as npimport pandas as pddf = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
which produces:
A B C D0 45 88 44 921 62 34 2 862 85 65 11 313 74 43 42 564 90 38 34 935 0 94 45 106 58 23 23 60.. .. .. .. ..
The recommended way to create random integers with NumPy these days is to use numpy.random.Generator.integers
. (documentation)
import numpy as npimport pandas as pdrng = np.random.default_rng()df = pd.DataFrame(rng.integers(0, 100, size=(100, 4)), columns=list('ABCD'))df---------------------- A B C D 0 58 96 82 24 1 21 3 35 36 2 67 79 22 78 3 81 65 77 94 4 73 6 70 96... ... ... ... ...95 76 32 28 5196 33 68 54 7797 76 43 57 4398 34 64 12 5799 81 77 32 50100 rows × 4 columns