Jhiems
Jhiems

Reputation: 11

Finding Max value in a column of csv file PYTHON

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!

Upvotes: 1

Views: 16773

Answers (2)

Python NWB
Python NWB

Reputation: 53

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.

Upvotes: 2

AChampion
AChampion

Reputation: 30258

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

Upvotes: 5

Related Questions