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 Cleaning Data Filtering Rows

Sahar Nasiri
Sahar Nasiri
7,454 Points

filtered_rows.append(data_sample[0])

Why does she append the header to the filtered_rows list?

from s3v1 import *

def filter_col_by_string(data_sample, field, filter_condition):
    filtered_rows = []

    col = int(data_sample[0].index(field))
    filtered_rows.append(data_sample[0])

    for item in data_sample[1:]:
        if item[col] == filter_condition:
            filtered_rows.append(item)

    return filtered_rows

silk_ties = filter_col_by_string(data_from_csv, "material", "_silk")
print("Found {} silk ties".format(number_of_records(silk_ties)))

wool_ties = filter_col_by_string(data_from_csv, "material", "_wool")
print("Found {} wool ties".format(number_of_records(wool_ties)))

cotton_ties = filter_col_by_string(data_from_csv, "material", "_cotton")
print("Found {} cotton ties".format(number_of_records(cotton_ties)))


gucci_ties = filter_col_by_string(data_from_csv, "brandName", "_Gucci")
print("Found {} Gucci ties".format(number_of_records(gucci_ties)))

1 Answer

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

So far, it appears that she uses logic to search for data by the column name, not the number. By adding a heading, if she searches in the future, she won't need to know the column number, just its heading.

col = int(data_sample[0].index(field))