2

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: 

is there a way to solve this problem with numpy or pandas ?

1
  • I think you should look at the root of your problem: how you read in the CSV file. Because this should be able to handle this at once and reading it in as floats. Can you give an extract of how your CSV file looks like? Commented May 11, 2013 at 14:46

2 Answers 2

3

I think there is an empty string('') in your array. So, change your array's '' into 0s.

Assuming your array is a:

>>> a[a=='']='0'
>>> a2 = a.astype(np.float)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.