Python 应用函数具有多个参数的lambda
我有以下功能。这里有lambda,它接受多个参数行和浮点值。当我运行时,我发现错误,原因是什么Python 应用函数具有多个参数的lambda,python,pandas,Python,Pandas,我有以下功能。这里有lambda,它接受多个参数行和浮点值。当我运行时,我发现错误,原因是什么 def func(): Top15 = create_frame() total_citations = Top15['Citations'].sum() ratioof_selfcitations_to_totalcitations = Top15.apply(lambda (row, total): (row['Self-citations']/total),
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您似乎需要:
ratioof_selfcitations_to_totalcitations = Top15.apply(lambda row: row['Self-citations']/total_citations, axis=1)
但更好更快的方法是将系列
除以标量
:
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
什么错误?您使用的是哪种版本的python?