Are you stuck with this Python nameerror name df is not defined error message?
We understand that this error be frustrating, especially if you don’t know how to fix it.
In this article, we will explore the what nameerror: name ‘df’ is not defined error is and why it occurs.
And we’ll guide how you can fix this error.
What is “nameerror name df is not defined”?
The nameerror: name ‘df’ is not defined error message is raised when you try to access the df (DataFrame) variable or object that has not been defined in your code.
This error message indicates that the interpreter could not find the variable or object you are trying to access.
The reason is that the variable or object has not been defined yet, or because it has been defined in a different part of your code.
Why does this error occur?
The nameerror name ‘df’ is not defined error message can occur or several reasons, including:
❌ The variable or object has not been defined yet.
❌ If you are accessing a df variable that doesn’t exist.
❌ Using a df variable before it is declared.
❌ The variable or object was defined in a different part of your code.
❌ The variable or object was misspelled or typed incorrectly.
❌ The variable or object was deleted or removed from your code.
How to fix “nameerror name df is not defined”?
To fix the nameerror: name ‘df’ is not defined, ensure to declare the df or define the variable before accessing it.
Solution 1: Define the variable before accessing it
To fix this error, you need to define the variable or object before you try to use it.
For example:
✅ df = 100
print(df)Output:
100Solution 2: Pass the Variable or Object
If you are trying to use a variable or object from a library or module, you need to import it first. Make sure that you have imported the library or module correctly.
For example:
✅ import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6], 'D': [7, 8]})
print(df)Output:
A B C D
0 1 3 5 7
1 2 4 6 8When the variable or object is defined in a different part of your code, you can pass it as an argument to the function where you are trying to use it.
Solution 3: Check for typos
Ensure that you have not misspelled the variable name when declaring or accessing it.
Solution 4: Verify the variable scope
If you have declared the df variable inside a function, make sure that you are accessing it within the same function.
Conclusion
The nameerror: name ‘df’ is not defined error message is raised when you try to access the df (DataFrame) variable or object that has not been defined in your code.
To fix the error you have to declare the df or define the variable before accessing it.
This article already provides solutions for this error to help you fix the Python error message.
You could also check out other “nameerror” articles that may help you in the future if you encounter them.
- Nameerror name ‘x’ is not defined
- Nameerror name np is not defined
- Nameerror name ‘raw_input’ is not defined
Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Frequently Asked Questions
What is Python NameError and what causes it?
NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.
How do I fix ‘name X is not defined’?
Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.
Why does my variable work in one cell but not another (Jupyter)?
Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.
What is the difference between NameError and AttributeError?
NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.
Where can I find more NameError fixes?
Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.
