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
• 12,150 points
31 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.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 7,560 points

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 ANMOL
• 3,620 points
13 views
0 votes
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,000 points
186 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 5, 2018 in Data Analytics by darklord
• 6,140 points
8 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,010 points
20 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,010 points
36 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
20 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.