Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'pd' is not defined

I am attempting run in Jupyter

import pandas as pd                     
import matplotlib.pyplot as plt          # plotting
import numpy as np                       # dense matrices
from scipy.sparse import csr_matrix      # sparse matrices
%matplotlib inline

However when loading the dataset with

wiki = pd.read_csv('people_wiki.csv')
# add id column
wiki['id'] = range(0, len(wiki))
wiki.head(10)

the following error persists

NameError                                 Traceback (most recent call last)
<ipython-input-1-56330c326580> in <module>()
----> 1 wiki = pd.read_csv('people_wiki.csv')
      2 # add id column
      3 wiki['id'] = range(0, len(wiki))
      4 wiki.head(10)

NameError: name 'pd' is not defined

Any suggestions appreciated

like image 587
dbldee Avatar asked Nov 03 '16 16:11

dbldee


People also ask

Why is pandas not defined?

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: Go to the top of your script and make sure you actually imported pandas.

How do you define a NameError in Python?

NameError is a kind of error in python that occurs when executing a function, variable, library or string without quotes that have been typed in the code without any previous Declaration. When the interpreter, upon execution, cannot identify the global or a local name, it throws a NameError.

How do you fix a name error in Jupyter notebook?

To solve this issue you have to just run the cell first that has import pandas as pd statement. Then run the other cell. It will clearly remove the nameerror name pd is not defined error. You can see you are now not getting any error.


2 Answers

Select Restart & Clear Output and run the cells again from the beginning.

I had the same issue, and as Ivan suggested in the comment, this resolved it.

like image 154
martin-martin Avatar answered Sep 20 '22 22:09

martin-martin


If you came here from a duplicate, notice also that your code needs to contain

import pandas as pd

in the first place. If you are using a notebook like Jupyter and it's already there, or if you just added it, you probably need to re-evaluate the cell, as suggested in the currently top-voted answer by martin-martin.

like image 36
tripleee Avatar answered Sep 19 '22 22:09

tripleee