我有以下功能。这里我有lambda,它接受多个参数row和float值。我正在跑步时,我发现错误的原因是什么?
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
答案 0 :(得分:1)
似乎你需要:
ratioof_selfcitations_to_totalcitations = Top15.apply(lambda row: row['Self-citations']/total_citations, axis=1)
但更好更快的是Series
除以scalar
:
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