1

I'm fairly new to coding, and I'm stuck on a current project. I have a .csv file, and rather than use a spreadsheet, I'm trying to write a python program to find the maximum value of a specific column. So far I have the following:

import csv

with open('american_colleges__universities_1993.csv', 'rU') as f:
    reader = csv.reader(f)
    answer = 0
    for column in reader :
        answer = 0
        for i in column[14]:
            if i>answer:
                answer = i
print answer

I keep getting something like:

9

The problem is that this is only returning the largest integer (which happens to be 9), when it should be returning something like 15,000. I suspect the program is only looking at each digit as its own value... How can I get it to look at the entire number in each entry?

Sorry for the newb question. Thanks!

2 Answers 2

5

Currently you are comparing each character in column[14] and setting the lexically maximum character to answer. Assuming you want to arithmetically compare the whole of column[14] you will need to replace the comma with '' and convert to int, e.g.:

with open('american_colleges__universities_1993.csv', 'rU') as f:
    reader = csv.reader(f)
    next(reader)     # Skip header row
    answer = max(int(column[14].replace(',', '')) for column in reader)
print answer

If you need the whole row that has the maximum column[14] you could alternatively use the key argument to max:

    answer = max(reader, key=lambda column: int(column[14].replace(',','')))
print answer
Sign up to request clarification or add additional context in comments.

2 Comments

So, the first row in the column is text. How can I skip it to look at the actual number? I've tried next(f)
You can next(reader) before the max call to skip the header line. Alternatively you can look into DictReader and then use column['ColumnName'].
2
import csv
with open('american_colleges__universities_1993.csv', 'r') as f:
    reader = csv.reader(f)
    maxnum = max(reader, key=lambda row: int(row[14]))
print(maxnum)

This should do the work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.