Home » How to Fix: NameError name ‘pd’ is not defined

How to Fix: NameError name ‘pd’ is not defined

by Tutor Aspire

One common error you may encounter when using Python is:

NameError: name 'pd' is not defined

This error usually occurs when you import the python library pandas, but fail to give it the alias of pd when importing it.

The following examples illustrate how this error occurs in practice and how you can quickly fix it.

Example 1: Use import pandas as pd

Suppose you import the pandas library using the following code:

import pandas

If you then attempt to create a pandas DataFrame, you’ll get the following error:

#create pandas DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#attempt to print DataFrame
print(df)

Traceback (most recent call last):
      1 import pandas
----> 2 df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
      3                    'assists': [5, 7, 7, 9, 12, 9, 9, 4],
      4                    'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
      5 

NameError: name 'pd' is not defined

To fix this error, you need provide the alias of pd when importing pandas

import pandas as pd

#create pandas DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#print DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

Example 2: Use import pandas

Suppose you import the pandas library using the following code:

import pandas

If you then attempt to create a pandas DataFrame, you’ll get the following error:

#create pandas DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#attempt to print DataFrame
print(df)

Traceback (most recent call last):
      1 import pandas
----> 2 df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
      3                    'assists': [5, 7, 7, 9, 12, 9, 9, 4],
      4                    'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
      5 

NameError: name 'pd' is not defined

To fix this error, you could simply choose not to use the alias of pd at all:

import pandas

#create pandas DataFrame
df = pandas.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#print DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

Note: The syntax “import pandas as pd” is commonly used because it offers a more concise way to use pandas functions. Instead of typing “pandas” each time, you can simply type in “pd” which is quicker and easier to read.

Additional Resources

How to Fix: No module named pandas
How to Fix: NameError name ‘np’ is not defined

You may also like