Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Data Science Basics Describing Data Calculating Sums and Totals

tracy rohlin
tracy rohlin
3,793 Points

Cannot get the prices from my_csv['priceLabel']

I'm using python 2.7. Here is my code:

FIELDNAMES = ['', 'id', 'priceLabel', 'name', 'brandId', 'brandName', 'imageLink',     'desc', 'vendor', 'print', 'material']
   DATATYPES = numpy.dtype([
       ('myint', 'i'),
       ('myid', 'i'),
       ('price', 'f8'),
       ('name', 'a200'),
       ('brandId', '<i8'),
       ('brandName', "a200"),
       ('imageURL', "|S500"),
       ('description', '|S900'),
       ('vendor', '|S100'),
       ('pattern', '|S50'),
       ('material','|S50'),
   ])


   def load_data(filename, d="\t"):
       my_csv = numpy.genfromtxt(filename, delimiter=d, skip_header=1,  invalid_raise=False, names='FIELDNAMES',
                              dtype=DATATYPES)
       return my_csv


my_csv = load_data('data.csv')
#print my_csv["priceLabel"]

I was trying to follow along with calculating the sum of prices but it keeps telling me

    print my_csv["priceLabel"]
ValueError: field named priceLabel not found

1 Answer

Frederick Pearce
Frederick Pearce
10,677 Points

I was able to run this in workspaces (Python 3.4.1) after adding an "import numpy" to the top and cleaning up some spacing issues. The problem seems to be that you have quotation marks around FIELDNAMES when calling bumpy.genfromtxt (i.e. names='FIELDNAMES' should be names=FIELDNAMES). No error running print(my_csv["priceLabel"]) after that fix, it prints a list of floats as expected

import numpy

FIELDNAMES = ['', 'id', 'priceLabel', 'name', 'brandId', 'brandName', 'imageLink', 'desc', 'vendor', 'print', 'material']

DATATYPES = numpy.dtype([
       ('myint', 'i'),
       ('myid', 'i'),
       ('price', 'f8'),
       ('name', 'a200'),
       ('brandId', '<i8'),
       ('brandName', "a200"),
       ('imageURL', "|S500"),
       ('description', '|S900'),
       ('vendor', '|S100'),
       ('pattern', '|S50'),
       ('material','|S50')])


def load_data(filename, d="\t"):
    my_csv = numpy.genfromtxt(filename, delimiter=d, skip_header=1, invalid_raise=False, names=FIELDNAMES, dtype=DATATYPES)
    return my_csv


my_csv = load_data('data.csv')
print(my_csv["priceLabel"])