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

28 ties

Why do I get 28 ties instead of 38 ties for under_20_bucks?

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

def filter_by_float(data_sample, field, direction, filter_condition):
    filtered_rows = []
    col = data_sample[0].index(field)
    cond = float(filter_condition)

    for row in data_sample[1:]:
        element = float(row[col])
        if direction == '<':
            if element < cond:
                filtered_rows.append(element)
        if direction == '>':
            if element < cond:
                filtered_rows.append(element)
        if direction == '<=':
            if element < cond:
                filtered_rows.append(element)
        if direction == '>=':
            if element < cond:
                filtered_rows.append(element)
        if direction == '==':
            if element < cond:
                filtered_rows.append(element)
        else:
            pass

    return filtered_rows


under_20_bucks = filter_by_float(data_from_csv, "priceLabel", "<=", 20)
print("Found {} ties < 20$".format(number_of_records(under_20_bucks)))

1 Answer

Kristian Gausel
Kristian Gausel
14,661 Points

I'm gonna make a wild guess and say the errors are here:

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

def filter_by_float(data_sample, field, direction, filter_condition):
    filtered_rows = []
    col = data_sample[0].index(field)
    cond = float(filter_condition)

    for row in data_sample[1:]:
        element = float(row[col])
        if direction == '<':
            if element < cond: #This is correct
                filtered_rows.append(element)
        if direction == '>':
            #if element < cond:   <- wrong try this:
            if element > cond:
                filtered_rows.append(element)
        if direction == '<=':
            #if element < cond:   <- wrong try this:
            if element <= cond:
                filtered_rows.append(element)
        if direction == '>=':
            #if element < cond:   <- wrong try this:
            if element >= cond:
                filtered_rows.append(element)
        if direction == '==':
            #if element < cond:   <- wrong try this:
            if element == cond:
                filtered_rows.append(element)
        else:
            pass

    return filtered_rows


under_20_bucks = filter_by_float(data_from_csv, "priceLabel", "<=", 20)
print("Found {} ties < 20$".format(number_of_records(under_20_bucks)))