Search code examples
pandasfor-looprandomscipypoisson

applying random distribution in each row of data frame


I have the following dataframe

import numpy as np
import pandas as pd
import scipy as sc
import scipy.stats as sct    
d= {'col1': [1, 2,5,0.6], 'col2': [3, 4,1,0.8]}
df = pd. DataFrame(data=d)

I want to add two new column in that dataframe but the element of two new columns are the random poisson distribution of col1 and col2 I used the following coding to generate the new columns (col3 and col4).

df ['col3']  = int(sct.poisson.rvs(df.col1,size=1))
df ['col4']  = int(sct.poisson.rvs(df.col2,size=1))

This is the closet example of my dataframe which is quite huge and it contains 3,800,000 rows. I can generate it using for loop. it took me too long time.

How can generate random poisson distribution based on dataframe without using loop? Thanks

Zep


Solution

  • Try just using:

    df['col3'] = sct.poisson.rvs(df.col1)
    df['col4'] = sct.poisson.rvs(df.col2)