我尝试从csv线获得平均值 . 我从字符串列表中的csv获取数据,进一步我将其转换为numpy数组 . 当我尝试绘制一些图形时,它的工作完美 . 但是,当我计算平均值时,我的数据会出现一些错误 .
如果我使用NumPy我得到:
TypeError: cannot perform reduce with flexible type
如果我使用统计库我得到:
TypeError: can't convert type 'string' to numerator/denominator
如果我在iPython上用comand'type'检查我的数组,我看到它是numpy.ndarray类型 .
我的阵列怎么了?你能解释一下,为什么转换numpy.asarray为matplotlib工作完美,但为不同的操作得到错误的类型 .
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))