The problem here is that you're building a list of the column-1 strings, but then expecting to find the max as a number, not as a string.
You could fix that by building a list of the column-1 strings mapped to integers, as other answers show:
for row in csvReader:
Revenue.append(int(row[1]))
max_revenue=max(Revenue)
But another way is to use a key function for max
:
for row in csvReader:
Revenue.append(row[1])
max_revenue = max(Revenue, key=int)
Even better, you can use the same idea to not need that whole separate Revenue
list:
max_revenue_row = max(csvReader, key=lambda row: int(row[1]))
This means you get the whole original row, not just the integer value. So, if, say, column 2 is the username that goes with the revenue in column 1, you can do this:
max_revenue_row = max(csvReader, key=lambda row: int(row[1]))
best_salesman_name = max_revenue_row[2]
This also avoids building a whole extra giant list in memory; it just reads each row into memory one at a time and then discards them, and only remembers the biggest one.
Which is usually great, but it has one potential problem: if you actually need to scan the values two or more times instead of just once, the first time already consumed all the rows, so the second time won't find any. For example, this will raise an exception in the second call:
max_revenue_row = max(csvReader, key=lambda row: int(row[1]))
min_revenue_row = min(csvReader, key=lambda row: int(row[1]))
The ideal solution is to reorganize your code to only scan the rows once. For example, if you understand how min
and max
work, you could build your own min_and_max
function that does both at the same time, and then use it like this:
min_revenue_row, max_revenue_row =
min_and_max(csvReader, key=lambda row: int(row[1]))
But sometimes that's not possible, or at least not possible in a way you can figure out how to write readably. I'll assume you don't know how to write min_and_max
. So, what can you do?
You have two less than ideal, but often still acceptable, options: Either read the entire file into memory, or read the file multiple times. Here's both.
rows = list(csvReader) # now it's in memory, so we can reuse it
max_revenue_row = max(rows, key=lambda row: int(row[1]))
min_revenue_row = min(rows, key=lambda row: int(row[1]))
with open(csvpath) as f:
csvReader = csv.reader(f)
max_revenue_row = max(csvReader, key=lambda row: int(row[1]))
with open(csvpath) as f:
# whole new reader, so it doesn't matter that we used up the first
csvReader = csv.reader(f)
min_revenue_row = min(csvReader, key=lambda row: int(row[1]))
In your case, if the CSV file is as small at it seems, it doesn't really matter that much, but I'd probably do the first one.