Skip to content

Python Pandas: How to Resolve "NameError: name 'pd' is not defined" Error

Fix the 'NameError: name 'pd' is not defined' in Python Pandas by ensuring the correct import statement (import pandas as pd) or using the full library name.

When working with the pandas library in Python, the NameError: name 'pd' is not defined is a very common error, especially for those new to the library. It occurs when your code attempts to use the conventional alias pd for pandas without having explicitly defined it in the import statement. The name pd is a community convention, not a built-in feature of Python.

The fix is simple: you must ensure that your import statement correctly creates the pd alias before it is used. This guide will explain the convention, show you how to fix the error, and address a common pitfall in Jupyter Notebooks.

Understanding the Error: The pd Alias Convention

In Python, when you import a library, you create a name in your current scope that refers to that library. The import pandas as pd statement does two things:

  1. It imports the pandas library.
  2. It creates a shorter name, or alias, pd, that you can use to refer to it.

The NameError occurs when Python encounters the name pd but has no definition for it in its memory. This happens if you only write import pandas but then try to use pd.

Reproducing the NameError

The error is triggered when there is a mismatch between how you import the library and how you use it.

Example of code causing the error:

import pandas

df = pd.DataFrame({"distance": [3.6, 18.3, 21.5, 25.2]})
print(df)

Output:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    df = pd.DataFrame({"distance": [3.6, 18.3, 21.5, 25.2]})
NameError: name 'pd' is not defined

The standard, community-accepted way to use pandas is to import it with the pd alias. This makes your code more concise and universally understood by other Python developers.

Solution:

import pandas as pd

df = pd.DataFrame({"distance": [3.6, 18.3, 21.5, 25.2]})
print(df)

Output:

   distance
0       3.6
1      18.3
2      21.5
3      25.2

Solution 2: Use the Full Library Name pandas

If you choose not to use an alias, you can fix the error by using the full library name, pandas, every time you call one of its functions or classes.

Solution:

import pandas

df = pandas.DataFrame({"distance": [3.6, 18.3, 21.5, 25.2]})
print(df)

Output:

   distance
0       3.6
1      18.3
2      21.5
3      25.2

While this works perfectly, it is more verbose and less common than using the pd alias.

Special Case: Jupyter Notebooks and Cell Execution Order

A common source of this error in Jupyter Notebooks or similar interactive environments is an issue with cell execution order. A notebook's state depends on which cells have been run and in what order.

If you have a cell containing import pandas as pd but have not executed it in your current session (or since you restarted the kernel), any subsequent cell that uses pd will fail with a NameError.

Solution: ensure you have run the import cell before running any other cells that depend on it.

Cell 1python
import pandas as pd
Cell 2python
df = pd.DataFrame({'col1': [1, 2]})
print(df)

If you still get the error, try selecting "Kernel" > "Restart & Run All" from the menu to ensure all cells are executed in the correct, top-to-bottom order.

Conclusion

The NameError: name 'pd' is not defined is a straightforward import issue.

If your code has... The fix is...
import pandas and pd.DataFrame() Change the import to import pandas as pd.
import pandas and you want to keep it Change the usage to pandas.DataFrame().
The error occurs in a Jupyter Notebook Make sure you have run the cell containing import pandas as pd.

To avoid this error and write code that is easy for others to understand, you should always follow the convention of importing the library with import pandas as pd at the top of your script.

Navigation