[英]Pandas: applying function to dataframe columns
I have dataframe 我有数据框
weight height
56 167
88 179
42 159
51 162
90 170
And I try to apply some function 我尝试应用一些功能
def min_error(w0, w1, height, weight):
return np.sum(np.power((height - (w0 + w1*weight))), 2)
(data.apply(lambda row: min_error(60, 0.05, row['Height'], row['Weight']), axis=1))
But it returns 但它返回
ValueError: ('invalid number of arguments', u'occurred at index 1') ValueError :(“无效数量的参数”,您在索引1处发生)
How can I fix that? 我该如何解决?
The problem is your call to np.power
. 问题是您致电np.power
。 You have the parenthesis in the wrong place. 您将括号放在错误的位置。 Try: 尝试:
def min_error(w0, w1, height, weight):
return np.sum(np.power((height - (w0 + w1*weight)), 2))
The problem is not with Pandas, but it was identified at a Pandas index, so it appeared to be an error with data.apply
, which it isn't. 问题不在于Pandas,而是在Pandas索引处发现的,因此data.apply
似乎是一个错误, data.apply
并非如此。
Your math formula is incorrect. 您的数学公式不正确。 What is happening here is that np.power expects two arguments but is only receiving 1. Check your parenthesis. 这里发生的是np.power需要两个参数,但只接收1个参数。检查括号。
I think this is the formula you want: 我认为这是您想要的公式:
def min_error(w0, w1, height, weight):
return np.sum(np.power((height - (w0 + w1*weight)),2))
(data.apply(lambda row: min_error(60, 0.05, row['height'], row['weight']), axis=1))
Output: 输出:
0 10857.6400
1 13133.1600
2 9389.6100
3 9890.3025
4 11130.2500
dtype: float64
也许这也会对您有帮助。
df['min_error']=min_error(60, 0.05, df['height'], df['weight'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.