Python - 将SciPy Beta Distribution应用于Pandas DataFrame的所有行

时间:2017-05-22 11:05:13

标签: python pandas scipy beta-distribution

在SciPy中,可以按如下方式实现beta分发:

x=640495496
alpha=1.5017096
beta=628.110247
A=0
B=148000000000 
p = scipy.stats.beta.cdf(x, alpha, beta, loc=A, scale=B-A)

现在,假设我有一个带有x,alpha,beta,A,B列的Pandas数据帧。如何将beta分布应用于每一行,将结果作为新列附加?

2 个答案:

答案 0 :(得分:2)

需要apply功能scipy.stats.beta.cdfaxis=1

df['p'] = df.apply(lambda x:  scipy.stats.beta.cdf(x['x'], 
                                                   x['alpha'], 
                                                   x['beta'], 
                                                   loc=x['A'], 
                                                   scale=x['B']-x['A']), axis=1)

样品:

import scipy.stats

df = pd.DataFrame({'x':[640495496, 640495440],
                   'alpha':[1.5017096,1.5017045],
                   'beta':[628.110247, 620.110],
                   'A':[0,0],
                   'B':[148000000000,148000000000]})
print (df)
   A             B     alpha        beta          x
0  0  148000000000  1.501710  628.110247  640495496
1  0  148000000000  1.501704  620.110000  640495440

df['p'] = df.apply(lambda x:  scipy.stats.beta.cdf(x['x'], 
                                                   x['alpha'], 
                                                   x['beta'], 
                                                   loc=x['A'], 
                                                   scale=x['B']-x['A']), axis=1)
print (df)
   A             B     alpha        beta          x         p
0  0  148000000000  1.501710  628.110247  640495496  0.858060
1  0  148000000000  1.501704  620.110000  640495440  0.853758

答案 1 :(得分:1)

鉴于我怀疑pandas apply只是循环遍历所有行,并且scipy.stats发行版在每次调用时都有相当多的开销,我会使用矢量化版本:

>>> from scipy import stats
>>> df['p'] = stats.beta.cdf(df['x'], df['alpha'], df['beta'], loc=df['A'], scale=df['B']-df['A'])
>>> df
   A             B     alpha        beta          x         p
0  0  148000000000  1.501710  628.110247  640495496  0.858060
1  0  148000000000  1.501704  620.110000  640495440  0.853758