Python Cannot Import Data using Pandas

0 votes

So, I tried to work through the following machine learning tutorial:

http://machinelearningmastery.com/machine-learning-in-python-step-by-step/

This is my (mac) development environment:

Python 2.7.10 
scipy: 0.13.0b1
numpy: 1.8.0rc1
matplotlib: 1.3.1
pandas: 0.20.2
sklearn: 0.18.1

When I try to run a script, to load the data from a URL containing the CSV data, I get the following error:

Traceback (most recent call last):
  File "load_data.py", line 4, in <module>
    dataset = pandas.read_csv(url, names=names)
NameError: name 'pandas' is not defined

Here's the script:

# Load dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)

Dec 7, 2018 in Data Analytics by Shubham
• 13,490 points
5,610 views
I have the  similar error. And how did you fix it? Thank you very much!

Hi. I had forgotten to import pandas library. I imported the library before my code and it worked fine:

import pandas

1 answer to this question.

0 votes

So< if your error says:

    dataset = pandas.read_csv(url, names=names)
NameError: name 'pandas' is not defined

It probably means you're trying to use pandas.read_csv() without importing Pandas first.When you want to use an external library, you have to import it. if it's not installed on your machine, you may have to install it first too. assuming pandas is installed on your machine, this code will work:

import pandas
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)

output:

dataset.head(3)

   sepal-length  sepal-width  petal-length  petal-width        class
0           5.1          3.5           1.4          0.2  Iris-setosa
1           4.9          3.0           1.4          0.2  Iris-setosa
2           4.7          3.2           1.3          0.2  Iris-setosa
answered Dec 7, 2018 by Upasana
• 8,620 points
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-da7c156ccdfe> in <module>
----> 1 import pandas
      2 url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
      3 names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
      4 dataset = pandas.read_csv(url, names=names)

ModuleNotFoundError: No module named 'pandas'

You are trying to import the pandas module but it is not installed on your system. To install pandas run the below command in the terminal:

pip install pandas

Related Questions In Data Analytics

0 votes
1 answer

Which package is used to do data import in R and Python and How do you import SAS data?

We can do data import using multiple ...READ MORE

answered Aug 24, 2018 in Data Analytics by Abhi
• 3,720 points
898 views
+1 vote
1 answer

Error saying "vector size cannot be NA" when using R with data mining

You can use the removesparseterm function.  Removes sparse ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
4,791 views
0 votes
1 answer
0 votes
1 answer

Join list of data.frames using map() call

You can use Reduce set.seed(24) r1 <- map(c(5, 10, 15), ...READ MORE

answered Apr 6, 2018 in Data Analytics by Sahiti
• 6,370 points
1,021 views
0 votes
1 answer

How to join a list of data frames using map()

You can use reduce set.seed(24) r1 <- map(c(5, 10, ...READ MORE

answered Apr 11, 2018 in Data Analytics by kappa3010
• 2,090 points
1,550 views
0 votes
1 answer

How to merge data frames using joins?

You can use the merge function with ...READ MORE

answered Apr 12, 2018 in Data Analytics by kappa3010
• 2,090 points
918 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,403 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP