Megan Byers
Megan Byers

Reputation: 169

PYTHON: Simplest way to open a csv file and find the maximum number in a column and the name assosciated with it?

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()

Upvotes: 1

Views: 1540

Answers (1)

joebeeson
joebeeson

Reputation: 4366

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])

Upvotes: 1

Related Questions