Converting list to array with NumPy asarray method
Question: I try get mean from csv line. I get data from csv in string list, further i
convert it to array with numpy. Its work perfect when i try plot some
graphics. But when i calculate mean i get some errors with my data.

If i use NumPy i get:

    
    
    TypeError: cannot perform reduce with flexible type
    

If i use statistics library i get:

    
    
    TypeError: can't convert type 'string' to numerator/denominator
    

If i check my array with comand 'type' on iPython i see that it numpy.ndarray
type.

Whats wrong with my array? Can you explain, why convert numpy.asarray for
matplotlib work perfect, but get wrong type for different operation.

    
    
    import csv
    import numpy as np
    import statistics as stat
    life_exp=[]
    with open('country.csv') as csvfile:
       datareader = csv.reader(csvfile)
       for row in datareader:
        if datareader.line_num!=1:
            life_exp.append(row[1])
    array_life_exp = np.asarray(life_exp) 
    print(stat.mean(array_life_exp))
    print(np.mean(array_life_exp))
    


Answer: Try this:

    
    
    from pandas import read_csv
    
    data = read_csv('country.csv')
    print(data.iloc[:,1].mean())
    

This code will convert your csv to pandas dataframe with automatic type
conversion and print mean of the second column.

