Nameerror name pd is not defined

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 pandas

If you are using Python 3:

pip3 install pandas

If you are using Jupyter Notebook:

✅ !pip install pandas


Take 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.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊