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 by Numeric Value

Andrew Winkler
Andrew Winkler
37,739 Points

Data Science, filter_col_by_float

can someone explain the variables of this function that we're supposed to use? It's much different than the ones that are featured in the videos or standard python libraries. I'm just not getting it.

filterByFloat.py
from loadData import *

data_from_csv = open_with_csv('data.csv')

solid_ties = filter_col_by_string(data_from_csv, "print", "_solid")

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

  col = int(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(row)
    elif direction == "<=":
      if element <= cond:
        filtered_rows.append(row)
    elif direction == ">":
      if element > cond:
        filtered_rows.append(row)
    elif direction == ">=":
      if element >= cond:
        filtered_rows.append(row)
    elif direction == "==":
      if element == cond:
        filtered_rows.append(row)  
    else:
      pass
  return filtered_rows

def filter_col_by_float(data_from_csv, solid_ties, solid35, price<35.00)
#I don't understand the arguments that this fuction takes.
# It appears to be a differant syntax than the video.

2 Answers

Challenge Task 1 of 1

We've stored a list with all the solid-colored ties in a variable named solid_ties for you. Use the function filter_col_by_float() to filter all solid ties under 35 dollars into the variable solid35. Hint: the field to filter on is called "priceLabel".

from loadData import *

data_from_csv = open_with_csv('data.csv')

solid_ties = filter_col_by_string(data_from_csv, "print", "_solid")

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

  col = int(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(row)
    elif direction == "<=":
      if element <= cond:
        filtered_rows.append(row)
    elif direction == ">":
      if element > cond:
        filtered_rows.append(row)
    elif direction == ">=":
      if element >= cond:
        filtered_rows.append(row)
    elif direction == "==":
      if element == cond:
        filtered_rows.append(row)  
    else:
      pass
  return filtered_rows

solid35 = filter_col_by_float(solid_ties, "priceLabel", "<", 35)
solid45 = filter_col_by_float(solid_ties, "priceLabel", ">", 45)

Looks like a custom filter function that is just looping through the values in a particular column in the specified data sample, and doing a numerical comparison with the operator and float value.

So, first, don't use the def keyword when you're calling/running/executing the function, only when you're defining it.

Second, I think solid_ties here is meant to be a subset of the entire data sample (data_from_csv) for you to test this function on.

So to find the solid ties with a price less than $35, you would use the following arguments:

data_sample = solid_ties
field = "price"
direction = "<"
filter_condition = 35.00  # or just 35

You would call the function like so:

filter_col_by_float(solid_ties, "price", "<", 35.00)

The arguments have to be in that particular order, since they are 'positional' arguments to the function.

Hope that helps!

Andrew Winkler
Andrew Winkler
37,739 Points

Got it. Those custom functions get me sometimes. Thank you