Applying poisson.cdf to create a column in a pandas dataframe

Viewed 21

I have a pandas dataframe with two columns, say x and y. For each row, x is the mean of a random variable following a poisson distribution. I want to add a third column, z, such that z = the probability that a random draw will be less than y.

For a given row, say x = 15 and I want to know the probability that a random draw will be less than y = 10. I know I can use:

from scipy.stats import poisson
x = 15
y = 10
z = poisson.cdf(y, x)
z

which returns 0.118

How do I do this for each row in a pandas dataframe, creating a third column?

1 Answers

You can use the apply method:

df = pd.DataFrame({"x": [15, 15, 15], "y": [10, 15, 20]})

df["z"] = df.apply(lambda r: poisson.cdf(r.y, r.x), axis=1)
print(df)

Result:

    x   y         z
0  15  10  0.118464
1  15  15  0.568090
2  15  20  0.917029
Related