NameError: name 'pd' is not defined in Python [Solved]

posted 6 min read

Introduction

If you are a newbie to Python and are working with Pandas module, you might get this error "NameError: name 'pd' is not defined". This error occurs when you are trying to work with Pandas without properly defining the alias as pd. The error is pretty straightforward, but it might leave you puzzled. No worries, in this article we will see what this error means, why it occurs, and how to solve it so let's get started.

Why did this error happen?

This error usually occurs while working with the Pandas library in Python. Consider the following code block.

 import pandas # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)

If you run the above code it will give you the below error.

The error "NameError: name 'pd' is not defined" suggests that the name pd is not defined which you are using in the code df = pd.DataFrame({...}). The Python interpreter cannot figure out what exactly pd means. Great, now you know why you are getting this error let's move further.

What is meant by an alias?

In the context of programming, an alias refers to an alternative name. It is a common practice for programmers to use aliases instead of actual names. Consider the following code.

import pandas # Importing pandas
  df = pandas.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)

In the above code we are importing pandas and then using it to create a data frame pandas.DataFrame({...}). Now what if we don't want to use the name pandas instead we want to use a short name that's where aliases come into the picture. Consider the code below.

  import pandas as pd # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)

Now in the above code, we have defined an alias name pd as import pandas as pd, and later we are using pd.DataFrame({...}) instead of pandas.DataFrame({...}). Now you know what an alias is and why programmers prefer to use an alias instead of simple imports so let's see the causes of this error.

What are the causes of the error?

1. Module not imported

If you have not imported the module import pandas as pd then you are going to get this error "NameError: name 'pd' is not defined". Consider the code below.

df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)

In the above code, you are trying to create a data frame but you have not imported the pandas module so it will result in the following error.

2. Alias not defined

One reason for getting this error "NameError: name 'pd' is not defined" is the alias is not properly defined. Take a look at the following code block.

import pandas # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)

After running the above code you will get the following error.

You are getting this error because you have not defined the alias pd that you are using in the code.

3. Using local scope

Another reason for getting this error "NameError: name 'pd' is not defined" is you have imported pandas within a local scope that will restrict its use. Consider the following code block.

def create(): # Function for creating a data frame
    import pandas as pd
    pd.DataFrame({'name': ['ali','uzair','asim'], 'marks': [10,20,30]})
   

create() # Calling the create function
pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame

By running the above code you will get the following error.

In this case, you have imported pandas with a proper alias import pandas as pd but still you are getting this error "NameError: name 'pd' is not defined" the reason is the local scope you have imported pandas inside a function and you are trying to create a data frame outside that function pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}).

Caution: When working with functions be cautious about importing Pandas within a local scope. Importing Pandas locally can restrict its availability

How to solve the error?

1. Solution for a module not imported

If you haven't imported the panda's module and you are trying to access it then you will get this error "NameError: name 'pd' is not defined". To solve it you just need to import the module. Consider the code below.

import pandas as pd # Importing the pandas module

df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)

After importing the module import pandas as pd the error will be removed. Below is the output of the above code.

You can use try except blocks for proper error handling.

Tip: Import the module first before using it.

2. Solution for alias not defined

The error "NameError: name 'pd' is not defined" clearly says that the name pd is not defined which simply means you are using pd as an alias but haven't defined it which can be simply resolved by defining pd as an alias. Consider the following code.

  import pandas as pd # Importing pandas and defining alias as pd
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating a data frame
  print(df)

Output of the above code.

Tip: Define the alias properly before using it.

3. Solution for local scope

In the case of local scope try importing pandas as global by doing that you can access it anywhere in your code. Consider the following code block.

import pandas as pd # Importing pandas in global scope

def create(): # Function for creating a data frame
    pd.DataFrame({'name': ['ali','uzair','asim'], 'marks': [10,20,30]})
   
create() # Calling the create function
df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)

Output of the above code.

Tip: Import the module in the global scope to access it anywhere in your code.

4. Solving the error in Jupyter Notebook or Google Colab

Sometimes you might be working in Jupyter Notebook or Google Colab in this case if you are getting this error "NameError: name 'pd' is not defined" check whether the alias pd is defined in global scope and if not define it and rerun your code.
If the alias is defined properly and you are still getting this error then it might be the following case.

where the import statement is in a separate cell and you are creating the data frame in a separate cell. In this case, run the previous cell first where you have imported Pandas, and then the cell where you are creating the data frame.

FAQ Q. What does the error message "NameError: name 'pd' is not defined" mean?
A. The error suggests that you are using an alias name 'pd' which is not properly defined in your code.

Q. How can I resolve the "NameError: name 'pd' is not defined" error?
A. To resolve it try importing 'import pandas as pd' in a global scope.

Conclusion

In summary, the error indicates that you are using an alias pd which is not defined in your code. To resolve this problem try to define the alias import pandas as pd in global scope first before using it. If you are working in Jupyter Notebook or Google Colab you have to define the alias as previous and also try to rerun the cell where you have imported pandas.

References

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

[PYTHON] Zipfile.badzipfile: file is not a zip file [SOLVED]

Muzzamil Abbas - Feb 14

TypeError: 'int' object is not iterable in Python

Ferdy - Oct 26, 2023

Pandas in Python: A Comprehensive Guide

Muzzamil Abbas - Apr 9

NumPy in Python: An Advanced Guide

Muzzamil Abbas - Mar 13

Attributeerror: 'dataframe' object has no attribute 'reshape'

Honey - Jun 20
chevron_left