2017-05-31 9 views
1

低い信頼区間の新しい列を行の他の値を使って作成しようとしています。私は信頼区間の計算をpublic-health-cisのパッケージpypiに書いています。これらの関数は浮動小数点値をとり、浮動小数点数を返します。Pandasのラムダ関数に列の値を渡す

私の分析スクリプトでは、私はこの機能をpandasデータフレームから呼び出そうとしています。私はいくつかのオプションを試してみると、これはうまく動作しません。

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'> 

私も使って試してみました:

applymap() got an unexpected keyword argument 'axis':エラーを実現し

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) 

軸kwargを取り除くと、最初の方法と同じエラーが発生します。では、各行の値を関数に渡して、それらの行のデータに基づいて値を取得するにはどうすればよいですか?

答えて

1

私はあなたが行によって、プロセスのためにaxis=1applyが必要だと思うので、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スカラーするindicator.rateを変更):

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

関連する問題