NameError: name 'df' or 'pd' is not defined in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. NameError: name 'df' is not defined in Python
  2. NameError: name 'pd' is not defined in Python

Note: if you got the error NameError: name 'pd' is not defined, click on the second subheading.

# NameError: name 'df' is not defined in Python

The Python "NameError: name 'df' is not defined" occurs when we try to access the df (DataFrame) variable before it is defined.

To solve the error, make sure to declare the df variable before accessing it.

nameerror name df is not defined

Here is an example of how the error occurs.

main.py
import pandas as pd # โ›”๏ธ NameError: name 'df' is not defined print(df) # ๐Ÿ‘‡๏ธ The definition is below df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } )

df is not defined in python

The issue is that we are trying to access the df variable before it was defined.

# Access the variable after it has been declared

To solve the error, make sure to access the variable after it has been defined.

main.py
import pandas as pd # โœ… 1) Define the variable df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) # โœ… 2) Access the variable print(df)

access variable after it has been declared

Make sure to import the pandas module before using it as shown in the code sample.

The error is also raised if you use the pandas module without having imported it.

# Common causes of the error

The error occurs for multiple reasons:

  1. Accessing a df variable that doesn't exist.
  2. Accessing a df variable before it is declared.
  3. Accessing a scoped variable from outside. For example, declaring a df variable in a function and trying to access it from outside.

Note: if you got the error NameError: name 'pd' is not defined, click on the following subheading.

# Trying to access a scoped variable from outside

Here is an example of trying to access a scoped variable from outside.

main.py
import pandas as pd def my_func(): # ๐Ÿ‘‡๏ธ df exists only in this function df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) my_func() # โ›”๏ธ NameError: name 'df' is not defined print(df)

We declared the df variable in a function, but we are trying to access it from the outer scope.

One way to solve the error would be to mark the variable as global.

main.py
import pandas as pd def my_func(): global df df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) my_func() # โœ… works print(df)

declare variable as global to solve error

Note that using the global keyword should generally be avoided as it makes our code harder to read and reason about.

An alternative approach would be to return the df variable from the function and store the result.

main.py
import pandas as pd def my_func(): df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) return df result = my_func() print(result)

return the dataframe from the function

Whichever approach you pick, make sure the df variable is available in the scope in which you're trying to access it.

# Make sure to not import the pandas module in a nested scope

Also, make sure you haven't imported pandas in a nested scope, e.g. a function.

main.py
def my_func(): import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) return df # โ›”๏ธ NameError: name 'pd' is not defined. Did you mean: 'id'? df2 = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } )

We imported the pandas module in a function, so we can't use it outside of the function.

Import the module at the top level to be able to use it throughout your code.

main.py
# โœ… Import at the top level of your file import pandas as pd def my_func(): df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) return df df2 = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } )

The import statement for the pandas module has to come at the top of the file before any code that makes use of it.

# Make sure to not import the pandas module in a try/except statement

You also should be importing the pandas module in a try/except statement.

main.py
try: # ๐Ÿ‘‰๏ธ Code here could raise an error import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) except ImportError: print(pd) print(pd)

The code sample works, however, if the code in the try statement raises an error, the pandas module won't get imported successfully.

This would cause the error because we are trying to access properties on the pandas module in the outer scope and the except block.

Instead, move the import statement to the top level of the file.

main.py
import pandas as pd try: df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) except ImportError: print(pd) print(pd)

# NameError: name 'pd' is not defined in Python

The Python "NameError: name 'pd' is not defined" occurs when we use the pandas module without importing it first.

To solve the error, install the module and import it (import pandas as pd) before using it.

nameerror name pandas is not defined

shell
NameError: name 'pd' is not defined in Python NameError: name 'pandas' is not defined in Python

Open your terminal in your project's root directory and install the pandas module.

shell
# ๐Ÿ‘‡๏ธ In a virtual environment or using Python 2 pip install pandas # ๐Ÿ‘‡๏ธ For python 3 (could also be pip3.10 depending on your version) pip3 install pandas # ๐Ÿ‘‡๏ธ If you get a permissions error sudo pip3 install pandas # ๐Ÿ‘‡๏ธ If you don't have pip in your PATH environment variable python -m pip install pandas # ๐Ÿ‘‡๏ธ For python 3 (could also be pip3.10 depending on your version) python3 -m pip install pandas # ๐Ÿ‘‡๏ธ For Anaconda conda install -c anaconda pandas

# Import the pandas module before using it

After you install the pandas module, make sure to import it before using it.

main.py
# โœ… Import pandas and alias it as pd import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)

The import statement imports the pandas module and aliases it to pd, so we access the module's classes as pd.DataFrame, pd.Series, etc.

# Misspelling the name of the module

Make sure you haven't misspelled pandas and are using all lowercase letters because module names are case-sensitive.

# Importing the module in a nested scope

Also, make sure you haven't imported pandas in a nested scope, e.g. a function. Import the module at the top level to be able to use it throughout your code.

# Make sure to use the correct import statement

If you got the error "NameError: name 'pandas' is not defined in Python", then you haven't aliased pandas as pd in your code and you should update the import statement.

main.py
import pandas df = pandas.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)

However, note that importing pandas and aliasing it as pd is much more common in modern code bases.

main.py
# ๐Ÿ‘‡๏ธ pandas aliased as pd import pandas as pd df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) print(df)

The import statement imports the pandas module and aliases it to pd, so we access the module's classes as pd.DataFrame, pd.Series, etc.

If the error persists after importing pandas as pd, restart your IDE and development server.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev