How to create a DataFrame of random integers with Pandas?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The usual way to build a Pandas DataFrame filled with random integers is to generate a NumPy array first and then wrap it in pd.DataFrame. Pandas itself is excellent at labels and tabular operations, but NumPy is the right tool for fast random number generation. Once you separate those roles, the code becomes simple and predictable.
Basic Example With NumPy and Pandas
What this does:
- '
default_rng(42)creates a reproducible random generator' - '
integersgenerates random integers in the half-open interval fromlowtohigh' - '
size=(4, 3)creates four rows and three columns' - '
pd.DataFrame(...)attaches tabular structure and column labels'
This is the clean default for most use cases.
Choose the Range Carefully
The upper bound is exclusive. That means low=0, high=10 produces values from 0 through 9, not 10.
This behaves like many Python range-style APIs, so it is worth remembering when you expect inclusive bounds.
Add Index Labels and Custom Columns
You can make the DataFrame more realistic by assigning labels.
This is useful for tests, demos, and synthetic datasets where the structure matters as much as the values.
Reproducibility Matters
If you want the same random integers each run, use a fixed seed. If you want different values every time, omit the explicit seed.
Reproducible:
Non-reproducible:
For notebooks, tests, and tutorials, fixed seeds are usually the right choice because they make examples stable.
Generate Specific Dtypes
The random integers are usually fine with the default integer dtype, but you can control it when needed.
This matters when memory size or downstream type expectations are important.
A One-Liner for Quick Use
For short scripts or ad hoc notebook work, a one-liner is often enough:
That is concise, though slightly less readable than a step-by-step version if the dataset shape and labels are important.
Use Cases Beyond Testing
Random integer DataFrames are useful for more than toy examples:
- load-testing a transformation pipeline
- building quick reproducible demos
- validating plotting or aggregation code
- generating placeholder tabular input during development
Just remember that synthetic randomness does not guarantee realistic distributions.
Common Pitfalls
- Expecting the upper bound passed to
integersto be included. - Using the old global random API when a local generator would be cleaner.
- Forgetting a seed when reproducibility matters.
- Generating the array first and then forgetting to add column names, making later code harder to read.
- Assuming random test data is representative of production data quality.
Summary
- Generate random integers with NumPy, then wrap the result in a Pandas DataFrame.
- '
np.random.default_rng().integers(...)is the modern NumPy API to prefer.' - The
highbound is exclusive. - Set a seed when you want reproducible output.
- Add labels and dtypes intentionally so the DataFrame matches your real use case.
Related reading
- How to create a decision boundary graph for kNN models in the Caret package?
- How to create a density plot
- How to create a dictionary of two pandas DataFrame columns
- How to create a neural network for regression?
- How to create a file name with the current date time in Python?
- How to create a GUID/UUID in Python
- How to create a Tensorflow Tensorboard Empty Graph
- How to Create Dataframe from AWS Athena using Boto3 get_query_results method
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.