Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Raychan
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.
Here is an example of how the error occurs.
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.
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:
df
variable that doesn't exist.df
variable before it is declared.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.
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
.
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)
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.
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.