Petr Petrov
Petr Petrov

Reputation: 4442

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')

How can I fix that?

Upvotes: 0

Views: 127

Answers (3)

Amir H
Amir H

Reputation: 125

maybe this will help you too.

df['min_error']=min_error(60, 0.05, df['height'], df['weight'])

Upvotes: 0

Scott Boston
Scott Boston

Reputation: 153460

Your math formula is incorrect. What is happening here is that np.power expects two arguments but is only receiving 1. Check your parenthesis.

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

Upvotes: 0

Jonathan Eunice
Jonathan Eunice

Reputation: 22443

The problem is your call to 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.

Upvotes: 1

Related Questions