1

I have looked at this reference so please do not refer me to it.

Find max number in .CSV file in Python I struggled to modify this code to my own document for over an hour with no results. The best I got from that code was printing out maximum value, and I got 0, which is incorrect. I need to 1) find the maximum value in column F, (38,332,521) and 2) print out the state associated with the maximum value (California).

Here is the csv I'm using: https://drive.google.com/open?id=0B29hT1HI-pwxMjBPQWFYaWoyalE

This is the code I've come up with myself, any help/feedback would be appreciated!

def largestState(): 
    INPUT  = "statepopulations.csv"
    COLUMN = 5   # 6th column

    with open(INPUT, "rU") as csvFile:
        theFile  = csv.reader(csvFile)
        header = next(theFile, None)    # skip header row
        pop = [float(row[COLUMN]) for row in theFile]

    max_pop = max(pop)
    print max_pop

largestState()

1 Answer 1

1

The max function should handle that for you. In my below code I've already "read" the CSV (with fake data, but you seem to have a grasp on that portion)

data = [
    ["California", 123456],
    ["Idaho", 123],
    ["Utah", 2]
]

print max(data, key=lambda _: _[1])

This yields ['California', 123456]

The key=lambda _: _[1] tells the function to use the second value of each record, the population in this case, to check the maximum.

Putting it all together should be something like:

def largestState():
    INPUT = "statepopulations.csv"
    COLUMN = 5
    with open(INPUT, "rU") as csvFile:
        data = csv.reader(csvFile)
        next(data, None)
    return max(data, key=lambda _: _[COLUMN])
Sign up to request clarification or add additional context in comments.

2 Comments

I plugged in your code and still have no luck, now I'm developing errors with "line containing NULL bytes". Google says its a problem with my csv, and I'm not sure how to work around this, as my professor will be grading with this exact file. Any ideas?
That sounds like another question

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.