Python 将列值传递到Pandas中的lambda函数

Python 将列值传递到Pandas中的lambda函数,python,pandas,lambda,Python,Pandas,Lambda,我试图使用行中的其他值为较低的置信区间创建一个新列。我已经在pypi上编写(并发布)了置信区间计算作为一个包public health cis。这些函数接受浮点值并返回浮点值 在我的分析脚本中,我试图从数据帧调用此函数。我尝试了几种方法试图让它工作,但没有效果 df_for_ci_calcs = df[['Value', 'Count', 'Denominator']].copy() df_for_ci_calcs = df_for_ci_calcs.applymap(lambd

我试图使用行中的其他值为较低的置信区间创建一个新列。我已经在
pypi
上编写(并发布)了置信区间计算作为一个包
public health cis
。这些函数接受浮点值并返回浮点值

在我的分析脚本中,我试图从数据帧调用此函数。我尝试了几种方法试图让它工作,但没有效果

    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))
通过此回溯返回:

内部服务器错误:/

这传递了错误:

applymap()得到一个意外的关键字参数“axis”

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

我认为您需要使用
axis=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)
示例(对于简化,我将
指标.比率
更改为标量
100
):


就这样,谢谢!我觉得自己像个白痴,因为我没有引用我发送的['Value']、['Count']等,所以我发送了整个系列,难怪它不喜欢它!
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)
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)
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