How to solve the NameError: name ‘pandas’ is not defined in Python

NameError: name 'pandas' is not defined in Python

“NameError: name ‘pandas’ is not defined” in Python is a popular error related to the library pandas. Let’s read the explanations below to know more about the causes of this error and solutions to fix it.

How does the NameError: name ‘pandas’ is not defined in Python happen?

This error happens due to two reasons. Essentially, it might be because you are calling a function which is from the pandas library but forgot to import them first. For example, calling pandas.Series() to get a series but not import pandas library first:

# Calling a function from pandas
print(pandas.Series(list()))

Output:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(pandas.Series(list()))
NameError: name 'pandas' is not defined

Second, if you have imported it but still got the error, it may be because you haven’t installed the package yet:

import pandas
 
# Calling a function from pandas
print(pandas.Series(list()))

Output:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import pandas
NameError: name 'pandas' is not defined

Have we made it clear about the reasons of this error. In the next section, we are going to solve them using two solutions.

How to solve the error?

As we have explained, you need the pandas library to implement the functions such as Series() or DataFrame() and many other functions (read this documentation). The first thing you have to do is installing the package pandas by typing this in the terminal:

sudo pip install pandas

If you are using Python, use this command instead:

sudo pip3 install pandas

If the error relating to the PATH environment occurs, you should type this command:

python -m pip install pandas

After we have successfully installed the package, all you have to do is declaring import pandas at the head of the program:

import pandas
 
# Calling a function from pandas
print(pandas.Series(list()))

Output:

Series([], dtype: float64)

If you want to reduce the pandas invoking when calling the method, you can import all the methods from the pandas package and call the method just like a normal function:

from pandas import *
 
# Calling a function from pandas
print(Series(list()))

Output:

Series([], dtype: float64)

The above code imports all the methods defined inside the pandas package so that you can reduce your code of calling the package before using the method. However, if you just want to use one or two methods from the package, you should change the * character with the method name, for instance:

from pandas import Series, DataFrame
 
# Calling a function from pandas
print(Series(list()))
print(DataFrame(None))

Output:

Series([], dtype: float64)
Empty DataFrame
Columns: []
Index: []

As you can see in the example below, you don’t have to call pandas.Series() or pandas.DataFrame() anymore. Because we have declared them in the import command, this could help you save time when coding because of shorter codes. However, don’t try to call the call pandas.Series() or pandas.DataFrame() as it will raise the same error:

from pandas import Series
 
# You must call Series() instead of pandas.Series()
print(pandas.Series(list()))

Furthermore, there are also some cases reported of encountering this error because of misspelling the word “pandas” as some may have used it as “Pandas” (case-sensitive). Therefore, you should recheck the spelling of this word carefully.

Summary

We have discussed how to deal with the error NameError: name ‘pandas’ is not defined in Python. You can solve it quickly by checking if the package pandas have been installed and imported before use and reading our other tutorials here. Good lucks for you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *