Page Visits Funnel Project Question

Hello everyone,

I have a question. I am currently working on the Page Visits Funnel Project. I completed it but then I experimented a bit and tried to define a function, that, given the df and column_name, automatically calculates the percent of users, who have not come to this step:

This is my code:

def pct_null(df,column_name):
return len(df[df.column_name.isnull()])/float(len(df))

pct_null(all_data,purchase_time)

You can see all_data here:
All_Data_Codeacademy

However, it always throws a mistake:

pct_null(all_data,purchase_time)
NameError: name ‘purchase_time’ is not defined

What did I do wrong? Thank you in advance :slight_smile:

From the information given the apparent issue is that you’re trying to reference a name purchase_time in a scope where it hasn’t been defined.

Perhaps you meant to pass a string instead? Consider carefully how to reference columns with pandas and whether using the .column notation is the best choice here.

I’d also suggest checking the following FAQ for a little detail on how best to set up a question (namely formatting code and linking the lesson of issue)-

Thank you very much for your answer, however I still don’t get it. Without defining a function this code works perfectly fine so I don’t really understand what went wrong. and what do you mean by “Meant to pass a string instead”? Why should I pass a string?

Sorry but I am still a bit confused :smiley:

You have a function that is currently written a little bit like the following-

def func(a, b):
    return a + b

x = 3
func(x, y)
Out: NameError: name 'y' is not defined

You can’t use a name that isn’t defined like the use of y above since it was never defined. You’re attempting to pass purchase_time but it’s never been assigned to anything.

On it’s own purchase_name is just a name or identifier for a variable, the name itself is not what you pass but the object that it refers to. For example if x = 3 then print(x) prints 3 and not x.

What’s more you can’t use names like this in your function either-

def func(lst, method):
    print(lst.method)

You’d just get an error that .method is not a recognised method. You’re swapping various data types about in your original function in a way that they just don’t work.

I’d suggest accessing your dataframe using the df['column name'] style and pass a string. Or alter your function so that you can pass df['column name'] as the argument to your function instead.