M511
M511

Reputation: 25

Add a vector/column of random numbers in Pandas

I've been trying to create a table that has randomly generated data using Pandas and Numpy. I've looked at the cheat sheet for Pandas but still can't get this work

import names
import pandas as pd
import random
import numpy as np

random.seed(100)
currency_numbers = random.sample(range(100000, 1000000), 100)

s = pd.Series(np.random.randn(100))
raw_data = {

    "Names":["".join(names.get_full_name()) for i in range(100)],

    "Names2":["".join(names.get_full_name()) for i in range(100)],

    "Currency":[]

}

df = pd.DataFrame(raw_data, columns=["Names", "Names2", "Currency"])

df.head()

I want to create a column of 100 random numbers under the Currency section?

Upvotes: 1

Views: 4007

Answers (2)

Drew Nicolette
Drew Nicolette

Reputation: 162

Just use the function: np.random.randint()

For example when I call this --> np.random.randint(1000,size=100)

The largest integer to be chosen in the random function is 999 aka anything from [0, 1000) and the size of the array would have a length of 100.

Therefore in your case,

s = np.random.randint(1000,size=100)

then set Currency to s,

"Currency":s

and the resulting DataFrame should give a column with 100 random numbers

JUST FYI, with this function you can also set a low and a high range... So in your case it would be something like this:

s = np.random.randint(100000, 1000000,size=100)

Upvotes: 2

Adrish
Adrish

Reputation: 52

Please check whether this helps.

import names
import pandas as pd
import random
import numpy as np

random.seed(100)
currency_numbers = np.random.randint(100000,1000000,size=(1,100))

s = pd.Series(np.random.randn(100))
raw_data = {

    "Names":["".join(names.get_full_name()) for i in range(100)],

    "Names2":["".join(names.get_full_name()) for i in range(100)],

    "Currency":currency_numbers[0]

}

df = pd.DataFrame(raw_data, columns=["Names", "Names2", "Currency"])

df.head()

Upvotes: 0

Related Questions