How to fix the Python nameerror name pd is not defined error message?
In this article, we will show you how you can resolve this error.
Aside from that, this article discusses what this error means and why it occurs in your Python script.
What is “nameerror name pd is not defined”?
The error message nameerror: name ‘pd’ is not defined occurs when you try to use the “pd” alias without importing the pandas library and defining the pd alias.
In addition to that, this error indicates that Pandas library is not properly imported or installed in your Python environment.
For example:
df = pd.DataFrame({'traffic 2022': [1, 2, 3, 4, 5], 'traffic 2023': [6, 7, 8, 9, 10]})
print(df)If you try to run this code automatically, it will throw an error message:
NameError: name 'pd' is not defined.In a nutshell, this error message is usually raised when you try to reference a variable or function from the Pandas library without importing it first.
What are the root causes of “nameerror: name pd is not defined”?
There are several reasons why this error appears in your Python script, here are some of them:
❌ Pandas library is not installed.
❌ Pandas library is not imported.
❌ Misspelling the Pandas library name or variable names.
How to fix “nameerror name pd is not defined”?
To fix the nameerror: name ‘pd’ is not defined error message, you have to import the pandas library and define the pd alias by using import pandas as pd.
Also, you can use the pandas library directly without using an alias.
Here are the following solutions which you can use to resolve the error:
Solution 1: Import pandas library and define “pd” alias
This solution fixes the error by importing the pandas library and define pd alias.
This allows you to use the pd alias to reference the pandas library and its functions in your code.
For example:
✅ import pandas as pd
df = pd.DataFrame({'traffic 2022': [1, 2, 3, 4, 5], 'traffic 2023': [6, 7, 8, 9, 10]})
print(df)Output:
traffic 2022 traffic 2023
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
Solution 2: Use pandas library directly without using an alias
You can use the pandas library directly without using an alias.
It will allow you to reference the pandas library and its functions in your code without using an alias.
For example:
✅ import pandas
df = pandas.DataFrame({'traffic 2022': [1, 2, 3, 4, 5], 'traffic 2023': [6, 7, 8, 9, 10]})
print(df)Output:
traffic 2022 traffic 2023
0 1 6
1 2 7
2 3 8
3 4 9
4 5 10
Solution 3: Check typos in your Python script
Ensure that you have not misspelled pandas or pd in your code.
Typos can sometimes be one of the major causes of errors like this.
Solution 4: Ensure you have installed the pandas library
Ensure that you have installed the pandas library. If you haven’t installed it yet, you can do it by executing the following command in your command prompt or terminal.
If you are using Python 2:
✅ pip install pandasIf you are using Python 3:
✅ pip3 install pandasIf you are using Jupyter Notebook:
✅ !pip install pandasTake note: If you are using a Jupyter notebook and have already imported the pandas library but are still getting the error.
You can try restarting the kernel and re-running all cells.
Conclusion
The error message nameerror: name ‘pd’ is not defined occurs when you try to use the “pd” alias without importing the pandas library and defining the pd alias.
This article already provides solutions that will help you to 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 unicode is not defined
- Nameerror: name os is not defined
- Nameerror: name self is not defined
Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Function-related NameError
NameError on a function usually means the function was not defined yet, the wrong name was used, or the function is in a different scope.
Common triggers
- Function name typo.
caclulate()instead ofcalculate(). - Called before def. At module level.
- Function in wrong module. Imported from a different module than expected.
- Decorated function shadowed. Decorator returned wrong name.
Diagnostic pattern
# BAD — undefined function
def main():
result = compute_total() # NameError if compute_total not defined
main()
# GOOD — check spelling and scope
def compute_total():
return 42
def main():
result = compute_total()
print(result)
main()
# For decorated functions, make sure functools.wraps is used
from functools import wraps
def my_decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)
return wrapper
Best practices
- Use editor autocomplete. Prevents most typo NameError.
- Order definitions before calls at module level.
- Use type hints. Editor flags undefined names immediately.
- Use functools.wraps in decorators to preserve the original function name.
Official documentation
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.
