从 csv 文件制作直方图

发布于 2024-12-25 08:21:07 字数 400 浏览 2 评论 0原文

我正在尝试从 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

知道发生了什么吗?

I am trying to read a column of data from a csv file and create a histogram for it. I could read the data into an array but was not able to make the histogram. Here is what I did:

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)

which gives me the error:

TypeError: cannot perform reduce with flexible type

Any idea what is going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2025-01-01 08:21:07

修改第六行,将字符串转换为数字,

    z.append(float(data[7]))

这样我就得到了一些我的虚构数据的图。

modify the sixth line to cast string to numeric

    z.append(float(data[7]))

with this i got some plot with my made up data.

画▽骨i 2025-01-01 08:21:07

这里有两个选项,如果您的所有列都由数字组成,则第一个选项会起作用:

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)

Here are two options, this one will work if all your columns are made up of numbers:

array = np.loadtxt('thimar.csv', 'float', delimiter=',')
n, bins, patches = plt.hist(array[:, 7], 50, normed=1)

this one is better if you have non-numeric columns in your file (ie Name, Gender, ...):

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文