Reputation: 119
I'm working with a csv file, I extract certain information given a condition, from it I have to obtain the average, but the result I obtain does not allow me to calculate the average, I tried converting it to integer and list but I am still in the same situation.
city=open('ciudades.csv')
lineas=csv.reader(city)
for a,name,countrycode,district, population in lineas:
if countrycode =='AFG':
print(population)
a=[population]
#a2=int(population)
b=a.mean()
print(b)
#print(a2)
when I print population I obtain a str like this
1780000
237500
186800
127800
this is my csv file looks like and i want he average from the country code= AFG, so when i print my population y have this and i can't have the average from that list
Upvotes: 0
Views: 56
Reputation: 36620
There is no mean
function on a list. You need to get these values in a list, then sum those values and divide by their length. A list comprehension will work nicely.
city = open('ciudades.csv')
lineas = csv.reader(city)
populations = [
int(population)
for a, name, countrycode, district, population in lineas
if countrycode == 'AFG'
]
avg = sum(populations) / len(populations)
Upvotes: 1