[Solved] NameError: name ‘pd’ is not defined

In Python,  NameError: name ‘pd’ is not defined occurs when you import the pandas library but fail to provide the alias as pd while importing it.

In this article, let us look at what is NameError name pd is not defined and how to resolve this error with examples.

Solution NameError: name ‘pd’ is not defined

Let us take a simple example to reproduce this error. In the below example, we have imported the pandas library and created a pandas DataFrame.

# import pandas library
import pandas 
import numpy as np

# create pandas DataFrame
df =  pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

# print dataframe
print(df)

Output

Traceback (most recent call last):
  File "C:\Personal\IJS\Code\main.py", line 6, in <module>
    df =  pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
NameError: name 'pd' is not defined. Did you mean: 'id'?

When we run the code, we get  NameError: name ‘pd’ is not defined  since we did not provide an alias while importing the pandas library.

There are multiple ways to resolve this issue. Let us look at all the approaches to solve the NameError.

Method 1 – Importing pandas with Alias as pd

The simplest way to resolve this error is by providing an alias as pd while importing the pandas library. Let’s fix our code by providing an alias and see what happens.

# import pandas library
import pandas as pd
import numpy as np

# create pandas DataFrame
df =  pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

# print dataframe
print(df)

Output

   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

The syntax “import pandas as pd” is commonly used because it offers a more concise way to call pandas functions, and the code is more readable as we do not have to type “pandas” each time.

Method 2 – Importing all the functions from pandas

There might be a situation where you need to import all the functions from the pandas library, and to do that, we will be using the below syntax.

from pandas import *

In this case, you do not need any reference to call any functions of pandas. You can directly call the methods without using an alias, and in this example we can directly create the DataFrame as shown below.

# import pandas library
from pandas import *
import numpy as np

# create pandas DataFrame
df =  DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

# print dataframe
print(df)

Output

   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

Method 3 – Importing pandas package without an alias

Another way is to import a complete pandas package and call the functions directly with the pandas name without defining an alias.

# import pandas library
import pandas
import numpy as np

# create pandas DataFrame
df =  pandas.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

# print dataframe
print(df)

Output

   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

In the above example, we import the complete pandas library and use pandas.DataFrame() method to create pandas DataFrame.

Note: If you are running the code in Jupyter notebook, ensure that you run the cell where you have imported the pandas and then run the rest of the code.
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like