How to fix the Nameerror name pd is not defined error?

Solve the name error pd not defined

Make sure to import the pandas library into your Python script or Jupyter notebook and assign it the alias pd before invoking any of the library methods:

import pandas as pd
your_df = pd.DataFrame()

Reproducing the name error

Suppose that you run the following snippet in your Jupyter Notebook, Pycharm , Spyder, VS Code or other Python dev environment:

# define lists of column values
month = ['September', 'November', 'March', 'September']
salary = [141.0, 140.5, 102.0, 153.0]
interviews = dict(month = month, salary = salary)

#create DataFrame
hr = pd.DataFrame(data=interviews)

This will render a NameError exception. Here’s a screenshot from my Jupyter notebook:

Understanding the error

The root cause is that you are trying to use the pandas library before declaring it in your code. First you need to import the library, and then if interested define an alias for the library that you will use in your code.

Fixing the error

First off, make sure to verify that pandas is installed in your Python environment. Then make sure to import pandas and define the pd alias as shown below

import pandas as pd
#rest of your code here
hr = pandas.DataFrame(data=interviews)

Note: Although defining an alias for pandas (and other Python packages) in your code helps readability, that is not mandatory. You could as well use other alias than ‘pd’, although that is the standard notation.

Same code without using the ‘pd’ alias:

import pandas
#rest of your code here
hr = pandas.DataFrame(data=interviews)

Related Learning

How to solve the no module found error in Numpy?