Search code examples
pythonnumpyloopsnormal-distribution

Trying to iterate np.random.normal over a list with a for loop


I'm trying to create a normal distribution for each value in a list and use a for loop as its 6,000 numbers. My code looks like:

for x in data:
   r[x]=np.random.normal(data['value'],data['Standard Deviation'],100000)

and I am getting the following error: ValueError: shape mismatch: objects cannot be broadcast to a single shape

I feel like there is probably something I am missing here due to my more entry level python knowledge and would sincerely appreciate any help. Thanks in advance!


Solution

  • Assuming that data is a pandas dataframe, you can try the following:

    r = np.random.normal(data['value'], data['Standard Deviation'], (100000, len(data))).T
    

    This will produce a 2-dimensional numpy array, each row of which will contain 100000 samples drawn from a normal distribution with mean and standard deviation given in the corresponding row of data.