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 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 12,909 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))