Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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"])