'Max() from a CSV column: ValueError: max() arg is an empty sequence
I am trying to open a csv and then us the columns to find totals and averages but I keep getting a value error when attempting to return the max height
with open('athlete_events_short.csv', 'r', newline='') as athletes:
reader = csv.reader(athletes)
header = next(reader)
for line in reader:
new_athlete = Athlete(line[1], line[2], line[3], line[4], line[5], line[6],
line[7], line[8], line[9], line[10], line[11], line[12], line[13], line[14])
athlete_list.append(new_athlete)
for athlete in athlete_list:
age_total += athlete.get_age()
weight_total += athlete.get_weight()
height_total += athlete.get_height()
max_height = max(reader, key=lambda column: (column[5].replace(',',''))) # this works but it returns a letter..not the max number
print(f'Average age in years for all olympians = {age_total/len(athlete_list):.2f}')
print(f'Average weight for all olympians = {weight_total/len(athlete_list):.2f} kilograms')
print(f'Average height for all olympians = {height_total/len(athlete_list):.2f} centimeters')
print(f'The tallest of all olympians is = {max_height:.2f} centimeters') # I can not get this to work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
