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:
You can see all_data here:
However, it always throws a mistake:
NameError: name ‘purchase_time’ is not defined
What did I do wrong? Thank you in advance
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)-
In general, your post will get a good answer if you’ve remembered to do the following:
Always search for existing answers first.
Provide as much information and context as possible.
Format your code.
Reference Additional Resources
Before Starting a Topic, Search for Existing Answers
Before you start a new topic, check existing posts and use Google to see if your questions have already been addressed. This will very often give you an immediate answer! Crypti…
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
You have a function that is currently written a little bit like the following-
def func(a, b):
return a + b
x = 3
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
3 and not
What’s more you can’t use names like this in your function either-
def func(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.