从CSV导入数据并将列转换为浮点型 [英] Numpy import from CSV and convert columns to float
本文介绍了从CSV导入数据并将列转换为浮点型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ok,所以我有从CSV文件加载numpy数组,
数组看起来像:
ok, so i have a numpy array loaded from a CSV file, the array looks like :
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')
我想将数组元素转换为浮动,但我有这个错误
I want to convert the array elements to float but i'm having this error
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?
is there a way to solve this problem with numpy or pandas ?
推荐答案
问题是一个特定的值,而不是Numpy或你的数据。我认为这是导致问题的空字符串,没有办法代表''
作为浮动。
The problem is with a specific value rather than Numpy or your data in general. I think it's the empty string that is causing the problem, there is no way to represent ''
as a float.
这篇关于从CSV导入数据并将列转换为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文