Last updated: Apr 8, 2024
Reading timeยท5 min
Note: if you got the error NameError: name 'pd' is not defined, click on the second subheading.
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) # ๐๏ธ The definition is below 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 # โ 1) Define the variable df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) # โ 2) Access the variable print(df)
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.
The error 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.Note: if you got the error NameError: name 'pd' is not defined, click on the following subheading.
Here is an example of trying to 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.
pandas
module in a nested scopeAlso, make sure you haven't imported pandas
in a nested scope, e.g. a
function.
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.
# โ 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.
pandas
module in a try/except statementYou also should be importing the pandas
module in a
try/except statement.
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.
import pandas as pd try: df = pd.DataFrame( { "Name": [ "Alice", "Bob", "Carl", ], "Age": [29, 30, 31], } ) except ImportError: print(pd) print(pd)
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 '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.
# ๐๏ธ 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
pandas
module before using itAfter you install the pandas module, make sure to import it before using it.
# โ 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.
Make sure you haven't misspelled pandas
and are using all lowercase letters
because module names are case-sensitive.
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.
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.
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.
# ๐๏ธ 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.