How to print the maximum value from a CSV data column?

Question:

I am trying to get the maximum value listed on a CSV file in one particular column. I cannot use pandas yet so everything has to be basic.

CSV sample data is:

grant 313  2014
grant 976  2013
grant 245  2012
grant 90   2011
grant 962  2010

Output needs to be: grant made 976 in 2013 –> which is the maximum value under the name grant and the year was made.

My code is:

import csv

Empmax = input("Enter employee name: ")
maxMade = 0
        
with open("data.csv") as file:
   reader = csv.reader(file)
    
   for line in reader:
      name = line[0]
      year = line[1]
      made = line[2]
        
      if Empmax.lower() == name.lower():
         if int(made) > maxMade:
         maxMade = int(made)
print(Empmax, "made", maxMade, "in", int(year))

Output comes out like this: grant made 962 in 2010.

Updated it and I got the max: I used: if int(made) > maxMade:
from benny’s example. Though year is still not updating.

Asked By: Wheng

||

Answers:

To follow your structure, you can do something like:

import csv
maxMade = 0
maxName = 'grant'
with open('data.csv', 'r') as f:
    csv_reader = csv.reader(f)
    for row in csv_reader:
        line = row[0].split()
        if line[0].lower() == maxName.lower():
          if int(line[1]) > maxMade:
             maxMade = int(line[1])
             maxLine = line
            
#print(maxLine)
print(maxLine[0], "made", maxLine[1], "in", maxLine[-1])

…will result in:

grant made 976 in 2013
Answered By: Bennimi

Assuming you really want to read a CSV file, you’ll want to change your data to be delimited by commas, like this:

grant,313,2014
grant,976,2013
grant,245,2012
grant,90,2011
grant,962,2010

Then, here’s a version of your code that gives you the results you desire:

import csv

empName = input("Enter employee name: ")
maxMade = 0
maxYear = 0

with open("/tmp/data.csv") as file:
    reader = csv.reader(file)

    for line in reader:
        name = line[0]
        made = int(line[1])
        year = line[2]

        if empName.lower() == name.lower() and made > maxMade:
            maxMade = made
            maxYear = year

    print(empName, "made", maxMade, "in", maxYear)

Result:

grant made 976 in 2013

If your data has spaces in it already and you don’t want to change that, then you should use a regular file reader and then call split() on each line of the file to get the individual fields. @BennyMue’s answer shows how to do this.

If you don’t use a CSV file and a CSV reader object, then I’d suggest that you change the extension on your data file and the names in your code to not use "csv", as doing that is misleading when you aren’t actually reading CSV data.

Answered By: CryptoFool

I got csv file data
2009 33 43 32 45
2010 45 90 56 33
2012 120 30 40 20
2013 110 15 20 30
2014 23 19 18 17
2015 22 22 24 25
2016 12 13 14 16

How to find average and maximum of six year span from 2010 to 2015?

Answered By: Puio
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.