If you are working with Python and trying to use the pandas library, you may encounter the “NameError: name ‘pandas’ is not defined” error. In this tutorial, we will explore why this error occurs and the steps required to fix it such that your Python code can successfully run without errors.
We will cover common causes of the error and provide solutions to help you get your code up and running quickly. So, let’s get started!
Why does the NameError: name 'pandas' is not defined
error occur?
This error occurs when you try to use the pandas library in your Python code, but Python cannot find the pandas module in its namespace. The following are some of the scenarios in which this error usually occurs.
- You have not imported the pandas module.
- You have imported the pandas module using a different name.
How to fix the NameError: name 'pandas' is not defined
error?
Before we proceed with the above scenario and their solutions, make sure that you have the pandas
module installed. If pandas
is not installed and you try to import it, you’ll get a ModuleNotFoundError
. Alternatively, you can check if you have pandas installed by running pip list
in your terminal or command prompt. If pandas is not listed, install it by running pip install pandas
.
If you are using a virtual environment or a different Python environment, make sure pandas is installed in that environment. You can activate the environment and run pip list
to check if pandas is installed.
Let’s now look at the above scenarios in detail.
The pandas
module is not imported
It can happen that you are trying to use the pandas
module without even importing it. In that case, you’ll get a NameError
. This is because Python does not recognize the pandas library and its functions until it is imported into the code.
For example, let’s try to use pandas
without importing it and see what we get.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# note that pandas is not imported # create a dataframe df = pandas.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 3 1 # note that pandas is not imported 2 # create a dataframe ----> 3 df = pandas.DataFrame({ 4 'Name': ['Jim', 'Pam', 'Dwight'], 5 'Age': [25, 26, 28] 6 }) 7 # display the dataframe 8 print(df.head()) NameError: name 'pandas' is not defined
We get a NameError
stating that the name pandas
is not defined. To use pandas
, you need to import it first.
import pandas # create a dataframe df = pandas.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
Here, we are importing the pandas
module first and then using it. You can see that we did not get any errors here.
Note that generally people use the import pandas as pd
statement to import the pandas module with the alias pd
and then use pd
instead of pandas
.
The pandas
module is imported using a different name
If you import the pandas module using a different name, for example import pandas as pd
, and then try to use the name “pandas” to use it, you will get a NameError
because the name “pandas” is not defined in your current namespace.
Let’s look at an example.
import pandas as pd # create a dataframe df = pandas.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import pandas as pd 3 # create a dataframe ----> 4 df = pandas.DataFrame({ 5 'Name': ['Jim', 'Pam', 'Dwight'], 6 'Age': [25, 26, 28] 7 }) 8 # display the dataframe 9 print(df.head()) NameError: name 'pandas' is not defined
We get a NameError: name 'pandas' is not defined
. This is because we have imported the pandas
module with the name pd
but we’re trying to use it using the name pandas
.
To fix this error, you can either access pandas using the name that you have used in the import statement or import pandas without an alias.
import pandas as pd # create a dataframe df = pd.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
In the above example, we are importing pandas
as pd
and then using pd
to access the pandas
module’s methods.
Alternatively, as seen in the example in the previous section, you can import pandas
without any aliases and simply use pandas
to avoid the NameError
.
How to fix the NameError: name 'pd' is not defined
error?
You can similarly get the NameError: name 'pd' is not defined
if you’ve not used pd
as the name for pandas while importing it. For example.
import pandas # create a dataframe df = pd.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 4 1 import pandas 3 # create a dataframe ----> 4 df = pd.DataFrame({ 5 'Name': ['Jim', 'Pam', 'Dwight'], 6 'Age': [25, 26, 28] 7 }) 8 # display the dataframe 9 print(df.head()) NameError: name 'pd' is not defined
We get this error because the name pd
does not exit in the namespace. To fix this error, use the import statement import pandas as pd
.
import pandas as pd # create a dataframe df = pd.DataFrame({ 'Name': ['Jim', 'Pam', 'Dwight'], 'Age': [25, 26, 28] }) # display the dataframe print(df.head())
Output:
Name Age 0 Jim 25 1 Pam 26 2 Dwight 28
We don’t get an error now.
Conclusion
In conclusion, the “NameError: name ‘pandas’ is not defined” error can occur due to two scenarios – pandas is not imported or pandas is imported with a different name. If pandas is not imported, you can simply import it using the command import pandas
. On the other hand, if pandas is imported with a different name, you can use the command import pandas as pd
to import it with the name “pd”. By following these steps, you can easily fix the “NameError: name ‘pandas’ is not defined” error and continue with your data analysis tasks using pandas.
You might also be interested in –