我有一個資料框,我想將一列中的每一行乘以另一列(列 Line Limit 是資料型別 float64)并回傳這些的總和。

def pol_stats(df,refcol,linecol,limitcol):
"""Summarise account file."""
acc_count = len(pd.unique(df[refcol]))
acc_exp = sum((df[linecol] / 100) * limitcol)
return (acc_count,acc_exp)
pol_stats(df,'Reference','Line','Limit')
但我收到錯誤'ufunc 'multiply' 不包含帶有簽名匹配型別的回圈 (dtype('<U32'), dtype('<U32')) -> dtype('<U32')'。
我試過在 limitcol 上使用float()或to_numeric()但仍然出現錯誤。如果兩列都是資料型別浮點數,不知道為什么這會是一個問題。
uj5u.com熱心網友回復:
問題是 column is df[limitcol]multiple by column name limitcol,所以用字串解決多個,所以引發錯誤:
acc_exp = ((df[linecol] / 100) * df[limitcol]).sum()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/369415.html
上一篇:如何計算熊貓資料框中的列?
