2

好的,所以我从 CSV 文件加载了一个 numpy 数组,该数组看起来像:

array([['0', '3', '22', ..., '7.25', '1', '0'],
       ['1', '1', '38', ..., '71.2833', '0', '0'],
       ['1', '3', '26', ..., '7.925', '1', '0'],
       ..., 
       ['0', '3', '', ..., '23.45', '1', '0'],
       ['1', '1', '26', ..., '30', '0', '0'],
       ['0', '3', '32', ..., '7.75', '0', '0']], 
      dtype='|S8')

我想将数组元素转换为浮点数,但出现此错误

data2 = np.array(data).astype(np.float)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: could not convert string to float: 

有没有办法用 numpy 或 pandas 解决这个问题?

4

2 回答 2

3

我认为您的数组中有一个空字符串('')。因此,将您的数组更改''0s。

假设您的数组是a

>>> a[a=='']='0'
>>> a2 = a.astype(np.float)
于 2013-05-11T14:00:26.580 回答
1

问题在于特定值,而不是 Numpy 或您的一般数据。我认为这是导致问题的空字符串,无法表示''为浮点数。

于 2013-05-11T12:58:57.790 回答