search
Search
menu search toc more_vert
Guest 0reps
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
help Ask a question
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
brightness_medium
share
arrow_backShare Twitter Facebook

# Creating a DataFrame with random numbers in Pandas

Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Creating DataFrames Cookbook
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags
expand_more

To create a DataFrame with random numbers in Pandas, use one of NumPy's functions that generate random numbers:

• `np.random.randint(~)` for random integers

• `np.random.default_rng().uniform(~)` for random floats

# DataFrame with random integers

First, we generate a 2 by 3 NumPy array of random integers:

``` arr_random = np.random.randint(low=2, high=10, size=(2,3))arr_random array([[8, 7, 2], [2, 3, 6]]) ```

Here, the lower bound is `2` (inclusive) and the upper bound is `10` (exclusive).

We can then initialise a DataFrame using `arr_random` like so:

``` pd.DataFrame(arr_random, columns=["A","B","C"], index=["a","b"]) A B Ca 4 4 2b 3 7 2 ```

# DataFrame with random floats

First, we generate a 2 by 3 NumPy array of random floats:

``` arr_random = np.random.default_rng().uniform(low=5,high=10,size=[2,3])arr_random array([[7.04518804, 9.09625941, 5.29815702], [7.77653952, 9.22222284, 9.0035309 ]]) ```

Note the following:

• all floats are larger than or equal to 5.

• all floats are less than 10.

To initialise a DataFrame using `arr_random`:

``` pd.DataFrame(arr_random, columns=["A","B","C"]) A B C0 7.927711 5.560577 5.9183311 8.487705 9.111051 9.699249 ```
mail