0

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.

Wheng
  • 11
  • 6
  • 1
    you didn't updated the year so it returns the last year, and you should print after for loop – Aisultan Jan 30 '21 at 00:15
  • What are the names of each of the CSV columns? – M-Chen-3 Jan 30 '21 at 00:18
  • What is `uses`? `max(int(made))` will fail because `max` requires two arguments. I expect that what you wanted was `maxMade = max(int(made), maxMade)` - you will still have a problem in that `year` won't be set to the year that had the maximum `made` value. As @Aisultan said, you probably don't want to print something every time you process a line of the file but rather call `print` only after the loop completes. – CryptoFool Jan 30 '21 at 00:23
  • @steve, I tried to do that maxMade = max(int(made), maxMade). I get error as well. – Wheng Jan 30 '21 at 00:35
  • My answer to [How to ignore the first line of data when processing CSV data?](https://stackoverflow.com/questions/11349333/how-to-ignore-the-first-line-of-data-when-processing-csv-data) (also) shows how to find the minimum, and it would be easy to modify it to find the maximum. – martineau Jan 30 '21 at 00:35
  • @M-Chen-3, it a big file of data. I only put grant since I only need to print one name. – Wheng Jan 30 '21 at 00:35
  • @Wheng - you likely still got an error because you were initializing `maxMade` to a list via `maxMade = []`. Passing that value into `max` will produce an error. If you set it to some initial integer value, you'd get a positive result. But you don't want to do it that way anyway, because you need to address getting the maximum year right as well, and that takes doing things a bit differently. See my answer, or @BennyMue's, both of which address the issue of keeping track of which year gave the maximum `made` value. – CryptoFool Jan 30 '21 at 00:45

2 Answers2

0

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
Bennimi
  • 416
  • 5
  • 14
0

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.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44