Turn Messy Text into Structured Insights With AI. Join Greg Live:Β Dec 4th, 2024
Leverage
Pandas errors

NameError name pandas is not defined, How To Fix

Pandas NameError resolution and aliases

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

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.

Link to code

On this page