Blue Week Special Offer | Brighten your week!
days
hours
minutes
seconds

Deterimin_value function

I try to create a function to determine value for each question. Error showed, I dont understand why my code dont work. Could someone help with it?

Screen Link: https://app.dataquest.io/m/210/guided-project%3A-winning-jeopardy/6/low-value-vs-high-value-questions

My Code:

def determin_value(row):
    for i in row['clean_value']:
        if i>800:
            value=1
        else:
            value=0
            
    return value

df['high_value'] = df.apply(determin_value, axis = 1)    

What I expected to happen:
my function works and returns a new column with a value 0 or 1

What actually happened:

TypeError: ("'int' object is not iterable", 'occurred at index 19325')
1 Like

@candiceliu93,

You shouldn’t use the for-loop here: the argument row of your function is already a row, i.e. there is no need to iterate through rows (you will apply this fuction to each row of your dataframe through using axis=1 in the apply() method).

Instead, inside your function you should start directly with the if-statement:

if row["clean_value"] > 800:

etc.

I see! I was not thinking right!! Thank you!

1 Like