Fix - NameError: name 'df' is not defined in Python

avatar

Borislav Hadzhiev

Wed Apr 20 20222 min read

banner

Photo by Raychan

Fix - 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) df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } )

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

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

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

The Python "NameError: name 'df' is not defined" 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.

Here is an example of trying ot 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)
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)

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

Use the search field on my Home Page to filter through my more than 1,000 articles.