我正在尝试从csv文件中读取一列数据并为其创建直方图。我可以将数据读入数组,但无法生成直方图。这是我做的:
thimar=csv.reader(open('thimar.csv', 'rb'))
thimar_list=[]
thimar_list.extend(thimar)
z=[]
for data in thimar_list:
z.append(data[7])
zz=np.array(z)
n, bins, patches = plt.hist(zz, 50, normed=1)
给了我错误:
TypeError: cannot perform reduce with flexible type
知道发生了什么事吗?
答案 0 :(得分:1)
修改第六行以将字符串转换为数字
z.append(float(data[7]))
有了这个,我得到了一些关于我的数据的情节。
答案 1 :(得分:0)
以下是两个选项,如果您的所有列都由数字组成,则此选项将起作用:
array = np.loadtxt('thimar.csv', 'float', delimiter=',')
n, bins, patches = plt.hist(array[:, 7], 50, normed=1)
如果您的文件中包含非数字列(即名称,性别等),则此更好:
thimar = csv.reader(open('thimar.csv', 'rb'))
thimar_list = list(thimar)
zz = np.array([float(row[7]) for row in thimar_list])
n, bins, patches = plt.hist(zz, 50, normed=1)