2017-07-09 10 views
-4

私は次のような機能を持っています。ここでは、複数の引数行と浮動小数点値をとるラムダがあります。私が走っている間、私は何が原因であるかを間違えているのですか?適用関数の引数が複数あるラムダ

def func(): 
    Top15 = create_frame() 
    total_citations = Top15['Citations'].sum() 
    ratioof_selfcitations_to_totalcitations = Top15.apply(lambda (row, total): (row['Self-citations']/total), 
                  total_citations) 
    return ratioof_selfcitations_to_totalcitations 

FUNC

+1

どのようなエラーが報告されましたか?あなたはPythonのバージョンを使用していますか? – Netwave

答えて

1

あなたが必要なようだ:

ratioof_selfcitations_to_totalcitations = Top15.apply(lambda row: row['Self-citations']/total_citations, axis=1) 

が、より良く、より速くはscalarで除算Seriesです:

ratioof_selfcitations_to_totalcitations = Top15['Self-citations']/total_citations 

サンプル:

Top15 = pd.DataFrame({'Self-citations': [1,2,3,6], 
         'Citations': range(4)}) 
print (Top15) 
    Citations Self-citations 
0   0    1 
1   1    2 
2   2    3 
3   3    6 

total_citations = Top15['Citations'].sum() 
ratioof_selfcitations_to_totalcitations = Top15.apply(lambda row: row['Self-citations']/total_citations, axis=1) 
print (ratioof_selfcitations_to_totalcitations) 
0 0.166667 
1 0.333333 
2 0.500000 
3 1.000000 
dtype: float64 

ratioof_selfcitations_to_totalcitations = Top15['Self-citations']/total_citations 
print (ratioof_selfcitations_to_totalcitations) 
0 0.166667 
1 0.333333 
2 0.500000 
3 1.000000 
Name: Self-citations, dtype: float64 
関連する問題