OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

obtain average from a data extracted from csv

  • Thread starter Thread starter Joan Lopez
  • Start date Start date
J

Joan Lopez

Guest
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.

Code:
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

Code:
1780000
237500
186800
127800

enter image description here

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
<p>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.</p>
<pre><code>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)
</code></pre>
<p>when I print population I obtain a str like this</p>
<pre class="lang-none prettyprint-override"><code>1780000
237500
186800
127800
</code></pre>
<p><a href="https://i.sstatic.net/bPSIbEUr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/bPSIbEUr.png" alt="enter image description here" /></a></p>
<p>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</p>
 

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top