2017-05-31 143 views
1

我正在尝试使用行中的其他值为较低置信区间创建一个新列。我已经(作为pypi上的包public-health-cis)编写(并发布)置信区间计算。这些函数采用浮点值并返回一个浮点数。将列值传递给Pandas中的lambda函数

在我的分析脚本中,我试图从熊猫数据框中调用这个函数。我尝试了几种方法来试图实现这一目标,但都无济于事。

df_for_ci_calcs = df[['Value', 'Count', 'Denominator']].copy() 
    df_for_ci_calcs = df_for_ci_calcs.applymap(lambda x: -1 if x == '*' else x) 
    df_for_ci_calcs = df_for_ci_calcs.astype(np.float) 
    df['LowerCI'].apply(lambda x: public_health_cis.wilson_lower(df_for_ci_calcs['Value'].astype(float), 
             df_for_ci_calcs['Count'].astype(float), 
             df_for_ci_calcs['Denominator'].astype(float), indicator.rate)) 

这种回溯回来:

Internal Server Error:/

df['LowerCI'].apply(lambda x: public_health_cis.wilson_lower(df_for_ci_calcs['Value'].astype(float), df_for_ci_calcs['Count'].astype(float), df_for_ci_calcs['Denominator'].astype(float), indica 
tor.rate)) 

TypeError: cannot convert the series to <class 'float'> 

我一直在使用也尝试:

df['LowerCI'] = df_for_ci_calcs.applymap(lambda x: public_health_cis.wilson_lower(df_for_ci_calcs['Value'], df_for_ci_calcs['Count'], 
                 df_for_ci_calcs['Denominator'], indicator.rate), axis=1) 

它提供了错误:

applymap() got an unexpected keyword argument 'axis'

当我将轴kwarg取出时,我得到与第一种方法相同的错误。那么,如何将每行的值传递给函数以获取基于这些行中数据的值?

回答

1

我认为你需要applyaxis=1由行的过程,所以获得输入作为float S:

df['LowerCI'] = df[['Value', 'Count', 'Denominator']] 
       .replace('*', -1) 
       .astype(float) 
       .apply(lambda x: public_health_cis.wilson_lower(x['Value'], 
                   x['Count'], 
                   x['Denominator'], 
                   indicator.rate), 
                   axis=1) 

样品(为简化我改变indicator.rate标量100):

df = pd.DataFrame({'Value':['*',2,3], 
        'Count':[4,5,6], 
        'Denominator':[7,8,'*'], 
        'D':[1,3,5], 
        'E':[5,3,6], 
        'F':[7,4,3]}) 

print (df) 
    Count D Denominator E F Value 
0  4 1   7 5 7  * 
1  5 3   8 3 4  2 
2  6 5   * 6 3  3 

df['LowerCI'] = df[['Value', 'Count', 'Denominator']] \ 
       .replace('*', -1) \ 
       .astype(float) \ 
       .apply(lambda x: public_health_cis.wilson_lower(x['Value'], 
                   x['Count'], 
                   x['Denominator'], 
                   100), axis=1) 

print (df) 
    Count D Denominator E F Value LowerCI 
0  4 1   7 5 7  * 14.185885 
1  5 3   8 3 4  2 18.376210 
2  6 5   * 6 3  3 99.144602 
+0

这就是它谢谢!我觉得自己是一个白痴,没有参考我发送的['Value'],['Count']等,所以我通过整个系列发送,难怪它不喜欢它! – RustyBrain

+1

很高兴能帮到你,祝你好运! – jezrael