I'm looking for a way to read this csv into python 2.7 and turn it into a (3,22000) array. For some reason I haven't been able to do it, no matter which way i try, I either get a groupn of strings in an array that i cant convert or an array seen below that won't convert to floats or allow computations to be done on them. Any help would be appreciated. Thanks
我正在寻找一种方法,将这个csv文件读入python 2.7,并将其转换为(3,22000)数组。由于某种原因,我无法做到这一点,不管我怎么尝试,我要么得到一个数组中的字符串,要么我不能转换,或者在下面看到的数组不会转换为浮点数,或者允许对它们进行计算。如有任何帮助,我们将不胜感激。谢谢
For the record it says the shape is (22000,), which I'm unsure about also.
据记录,它的形状是(22000,),我也不确定。
In [126]: import csv
import numpy as np
with open("Data.csv") as sd:
ri = []
dv = []
for row in csv.reader(sd):
if row != ["ccx","ccy","ccz","cellVolumes","Cell Type"]:
nrow = []
for val in row[0:3]:
val = float(val)
nrow.append(val)
ri.append(nrow)
nrow = []
for val in row[3:4]:
val = float(val)
nrow.append(val)
dv.append(nrow)
ri = np.array(ri)
ri
.
。
Out[126]: array([[-0.179967, -0.38936, -0.46127], [-0.0633236, -0.407683, -0.542979],
[-0.125841, -0.494202, -0.412042], ...,
[-0.0116821, 0.764493, 0.573541], [0.630377, 0.469657, 0.442017],
[0.248253, 0.615365, 0.354134]], dtype=object
1 个解决方案
#1
0
(from the helpful comments)
(从有用的评论)
Check the length of those sublists. If they are all the same I'd expect a 2d array; but if they differ (most 3, but some 0, 2,4 etc) then the best it can do is give you a 1d array of 'objects' - the lists.
检查这些子列表的长度。如果它们都是一样的,我期望一个2d数组;但是如果它们不同(大多数是3,但是0、2、4等等),那么它所能做的最好的事情就是给你一个1d的“对象”数组——列表。
I would just do [len(x) for x in ri]
before passing it to np.array
. Maybe apply a max
and min
. A list comprehension like that won't take long.
在将它传递给np数组之前,我只需要(len(x) for x)。也许可以使用max和min,这样的列表理解不会花太长时间。
#1
0
(from the helpful comments)
(从有用的评论)
Check the length of those sublists. If they are all the same I'd expect a 2d array; but if they differ (most 3, but some 0, 2,4 etc) then the best it can do is give you a 1d array of 'objects' - the lists.
检查这些子列表的长度。如果它们都是一样的,我期望一个2d数组;但是如果它们不同(大多数是3,但是0、2、4等等),那么它所能做的最好的事情就是给你一个1d的“对象”数组——列表。
I would just do [len(x) for x in ri]
before passing it to np.array
. Maybe apply a max
and min
. A list comprehension like that won't take long.
在将它传递给np数组之前,我只需要(len(x) for x)。也许可以使用max和min,这样的列表理解不会花太长时间。