NameError: name ‘pandas’ is not defined – How To Fix

Pandas NameError is frustrating. This error happens because your python script cannot find the name ‘pandas’ in your namespace. This means you have either imported pandas under a different name or not at all.

How to Fix:

  1. Go to the top of your script and make sure you actually imported pandas
  2. If you have, make sure you understand which alias you’re using. In this case import pandas as pd ‘pd’ is known as the alias.
1. import pandas >> python will recognize 'pandas'
2. import pandas as pd >> python will recognize 'pd'

Pseudo code: import pandas under the correct name

Pandas NameError

Pandas NameError - name 'pandas' is not defined. Easy to fix by making sure you imported pandas correctly!

In most cases, my guess is that you haven’t imported pandas to your python script. This is a simple mistake and has a simple answer, head to the top of your script and import it.

If this isn’t the case, then you likely imported it under a different alias that you’re not using

Pandas Aliases

When you import python packages, you have the option to specify an alias. As alias is an alternative string (usually a shorter string) that will be used in place of the default name of your package.

This is usually done as a shortcut to not type as many letters. For example pandas is usually referred to as ‘pd’, numpy as ‘np’, matplotlib.pyplot as ‘plt’ and so forth.

Let’s look at a code example of how to fix this.


Pandas Name Error

You might see the following error in your code: NameError: name 'pandas' is not defined

Pandas NameError can be annoying. It generally means you have not imported pandas, or if you did, you're not calling the right name or alias

Let's check out some examples:

  1. Invoking the error
  2. Fixing via 'pandas'
  3. Fixing w/ alias

First let's try to create a DataFrame...

In [2]:
pandas.DataFrame([('Foreign Cinema', 'Restaurant'),
                   ('Liho Liho', 'Restaurant'),
                   ('500 Club', 'bar'),
                   ('The Square', 'bar')],
           columns=('name', 'type')
                 )
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-20fdeccb36a4> in <module>
----> 1 pandas.DataFrame([('Foreign Cinema', 'Restaurant'),
      2                    ('Liho Liho', 'Restaurant'),
      3                    ('500 Club', 'bar'),
      4                    ('The Square', 'bar')],
      5            columns=('name', 'type')

NameError: name 'pandas' is not defined

Oh no! 'pandas' is not defined. Why is that? Well, let's see if we've imported pandas.

Checking...

Oh whoops, no we haven't. Let make sure to import it

In [3]:
import pandas

Ok let's try that again now that we've imported pandas

In [4]:
pandas.DataFrame([('Foreign Cinema', 'Restaurant'),
                   ('Liho Liho', 'Restaurant'),
                   ('500 Club', 'bar'),
                   ('The Square', 'bar')],
           columns=('name', 'type')
                 )
Out[4]:
nametype
0Foreign CinemaRestaurant
1Liho LihoRestaurant
2500 Clubbar
3The Squarebar

Nice! Now it works because python now has 'pandas' in it's namespace.

But sometimes I'm lazy, and 'pandas' is about 4 characters more than I want to type out. I'm going to use an alias.

Aliases are shorter strings that represent a longer string. Kind of like a variable, but for packages.

Here I'm going to use 'pd' (the most common alias of pandas) as an alias.

In [5]:
import pandas as pd

Now that I've done that, I'm able to use 'pd' instead of pandas when I'm calling the library

In [6]:
pd.DataFrame([('Foreign Cinema', 'Restaurant'),
                   ('Liho Liho', 'Restaurant'),
                   ('500 Club', 'bar'),
                   ('The Square', 'bar')],
           columns=('name', 'type')
                 )
Out[6]:
nametype
0Foreign CinemaRestaurant
1Liho LihoRestaurant
2500 Clubbar
3The Squarebar

If I wanted to use another alias, no problem, just define it in your import again.

In [7]:
import pandas as data_independent_is_awesome
In [8]:
data_independent_is_awesome.DataFrame([('Foreign Cinema', 'Restaurant'),
                   ('Liho Liho', 'Restaurant'),
                   ('500 Club', 'bar'),
                   ('The Square', 'bar')],
           columns=('name', 'type')
                 )
Out[8]:
nametype
0Foreign CinemaRestaurant
1Liho LihoRestaurant
2500 Clubbar
3The Squarebar

Link to code above

Check out more Pandas functions on our Pandas Page