BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Functions: Fundamentals Mission Practice Problems p.10

Hi guys, I have encountered a problem with the Functions: Fundamentals Mission Practice Problems. Please give me some advices:

Screen Link:
https://app.dataquest.io/m/1012/python-fundamentals-practice-problems/10/finding-column-indexes

My Code:

from csv import reader

def find_col_index(csv_filename, col_name):
    
    opened_file = open(filename)
    Reader = csv.reader(opened_file)
    All_rows = list(Reader)
    Headers = All_rows[0]
    
    if col_name in Headers:
        for col_index in range(len(Headers)):
            if col_name == Headers[col_index]:
                return col_index
    else:
        return -1
        
    
find_col_index(filename, col_name1)

What I expected to happen:
I tested my code with three examples and it is working nicely. I expected my code to work with the test.csv file.

What actually happened after submit answer:

"Executing your function resulted in an error. Error: [Errno 2] No such file or directory: '/dataquest/results/python-plot-jsons.json' Input: ['test.csv', 'h0']"

Can anyone tell me how to solve this problem?

Hello.

The following line of your code returns an error:

Reader = csv.reader(opened_file)

That is because when you run from csv import reader you import only the reader function from csv, therefore you cannot type csv.reader. This is enough:

Reader = reader(opened_file)

However, I do not know if this is the only problem as the error says No such file or directory. Try to fix this and let me know what happens.

thanks for your reply.
I try and change the program from csv.reader to reader. It is still not working. Is it possible that maybe there is no such file in the hidden directory?

I just saw the problem. The function receives the argument csv_filename:

def find_col_index(csv_filename, col_name):

But when you try to open the file you inform a different name:

opened_file = open(filename)

You should run open(csv_filename). That is how I did it:

def find_col_index(csv_filename, col_name):

opened_file = open(csv_filename)
read_file = reader(opened_file)
rows = list(read_file)
header = rows[0]

col_index = -1
for i in range(len(header)):
    if header[i] == col_name:
        return i
return col_index
1 Like

Thanks for your help :rofl:… I got it. I forgot to change the filename to csv_filename. Anyway, I think this practice mission is a very nice tutorial.

1 Like

I give up, I understand the answer provided by DQ

anyway, been toying with the function below, any idea why it fails?

 def find_col_index(csv_filename, col_name):
    opened_file = open(csv_filename)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    rangelist = range(len(data[0])-1)
    for c in rangelist:
        if data[0][c] == col_name:
            return c
        else:
            return -1```

Hi Octavios,

A quick question, is there a reason why we can’t use…
“if col_name in header:”
instead of “if col_name == header[i]”?

1 Like

Hi,

I’m not really sure what’s going on here, since it’s been a while since a replied to the topic.

But it seems to me that the idea is to return the index of the column that’s equal to the col_name argument.

If you use if col_name == header[i], this statement will only be true once and when it happens, the function will return the index i.

However, if you use if col_name in header, this will always be true (assuming that the column name you’re looking for exists) and then it will always return the first index, 0.

Hi,

Sorry we missed this. Could you explain more about what’s going wrong?