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 Exporting Exporting

Devin Scheu
Devin Scheu
66,191 Points

Exporting

Anyone got an answer for this one yet?

Code:

filterColumn.py
from openWithCsv import *

data_from_csv = open_with_csv('data.csv')

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

    #find index of field in first row
    col = int(the_data[0].index(field))
    filtered_rows.append(the_data[0])

    for row in the_data[1:]:
        if row[col] == filter_condition:
            filtered_rows.append([str(x).encode('utf8') for x in row])

    return filtered_rows

# YOUR CODE HERE

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

Yes:

dolce_gabbana = filter_col_by_string(data_from_csv, 'brandName', "Dolce & Gabbana")