首页 > 解决方案 > Using Truncated Poisson in Pandas data frame

问题描述

Withe help of @ Welcome to Stack Overflow, I managed to truncate the Poisson distribution using upper limit.When I used the function so called truncated Poisson, which is user defined function, it worked with single value entry, which I have shown in the code below:

import scipy.stats as sct
import pandas as pd
def truncated_Poisson(mu, max_value, size):
    temp_size = size
    while True:
        temp_size *= 2
        temp = sct.poisson.rvs(mu, size=temp_size)
        truncated = temp[temp <= max_value]
        if len(truncated) >= size:
            return truncated[:size]

mu = 2.5
max_value = 10
print(truncated_Poisson(mu, max_value, 1))

Unfortunately, I threw me an error when I applied it in the data frame as follow:

data = pd.DataFrame()
data['Name'] = ['A','B','C','D','E']
data ['mu']  = [0.5,1.2,2,2.5,2.8]
max_value = 5
size = 1 
data ['Pos'] =  truncated_Poisson(data.mu,max_value,size = 1)

The error statement is

ValueError: size does not match the broadcast shape of the parameters.

Can anyone advise me how to use that function in dataframe?

Thanks

Zep.

标签: pythonpandasdataframepoisson

解决方案


据我了解,您想truncated_Poisson使用相同的参数和mu数据中的每一个进行调用。例如,您可以使用以下方法执行此操作.apply

data['Pos'] = data.mu.apply(lambda mu: truncated_Poisson(mu, max_value, size=1))

>>> data
  Name   mu  Pos
0    A  0.5  [0]
1    B  1.2  [0]
2    C  2.0  [3]
3    D  2.5  [4]
4    E  2.8  [3]

推荐阅读