Using data from pythons pandas dataframes to sample from normal distributions
Question: I'm trying to sample from a normal distribution using means and standard
deviations that are stored in pandas DataFrames.

For example:

    
    
    means= numpy.arange(10)
    means=means.reshape(5,2)
    

produces:

    
    
       0  1
    0  0  1
    1  2  3
    2  4  5
    3  6  7
    4  8  9
    

and:

    
    
    sts=numpy.arange(10,20)
    sts=sts.reshape(5,2)
    

produces:

    
    
        0   1
    0  10  11
    1  12  13
    2  14  15
    3  16  17
    4  18  19
    

How would I produce another pandas dataframe with the same shape but with
values sampled from the normal distribution using the corresponding means and
standard deviations.

i.e. position `0,0` of this new dataframe would sample from a normal
distribution with `mean=0` and `standard deviation=10`, and so on.

My function so far:

    
    
        def make_distributions(self):
            num_data_points,num_species= self.means.shape
            samples=[]
            for i,j in zip(self.means,self.stds):
                for k,l in  zip(self.means[i],self.stds[j]):
                    samples.append( numpy.random.normal(k,l,self.n) )
    

will sample from the distributions for me but I'm having difficulty putting
the data back into the same shaped dataframe as the mean and standard
deviation dfs. Does anybody have any suggestions as to how to do this?

Thanks in advance.


Answer: You can use
[`numpy.random.normal`](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.random.normal.html)
to sample from a random normal distribution.  
IIUC, then this might be easiest, taking advantage of
[`broadcasting`](http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html):

    
    
    import numpy as np
    np.random.seed(1) # only for demonstration
    np.random.normal(means,sts)
    
    array([[ 16.24345364,  -5.72932055],
           [ -4.33806103, -10.94859209],
           [ 16.11570681, -29.52308045],
           [ 33.91698823,  -5.94051732],
           [ 13.74270373,   4.26196287]])
    

Check that it works:

    
    
    np.random.seed(1)
    print np.random.normal(0,10)
    print np.random.normal(1,11)
    
    16.2434536366
    -5.72932055015
    

If you need a pandas DataFrame:

    
    
    import pandas as pd
    pd.DataFrame(np.random.normal(means,sts))
    

