How to Create a Dataframe with Random Values in Python
To create a dataframe with random values in Python, you can use the numpy.random.randn() function from the numpy library.
The following example shows how to create a dataframe with random values in Python using the numpy.random.randn() function.
Using numpy.random.randn() Function
We can use the numpy.random.randn() function to create a dataframe with random values.
Suppose we want to create a dataframe with 5 rows and 3 columns:
import pandas as pd
import numpy as np
# Declare number of rows and columns
rows = 5
cols = 3
# Create random data
data = np.random.randn(rows, cols)
# Create dataframe
df = pd.DataFrame(data)
# Show dataframe
print(df)
Output: 👇️
0 1 2
0 -0.292078 -1.943827 0.615722
1 1.018362 0.644011 0.571727
2 -0.452343 0.730979 -0.312903
3 -1.011382 0.049792 0.672833
4 -1.625866 0.277710 0.162639
In this example, we use the numpy.random.randn() function to create random data with 5 rows and 3 columns.
We then use the pandas.DataFrame() function to create a dataframe df from the random data.
The output shows the dataframe with random values.
Conclusion
We can use the numpy.random.randn() function from the numpy library to create a dataframe with random values in Python.