0

I have a this multidimension array like this:

array([['120', '29.9475077984', '1'],
       ['125', '31.3887667742', '1'],
       ['125', '32.3881706091', '1'],
       ['125', '34.4894481007', '1'],
       ['126', '36.1494551046', '1'],
       ['127', '39.3121447948', '1'],
       ['128', '43.8203811171', '1'],
       ['128', '49.3179066095', '1'],
       ['128', '53.4929489926', '1'],
       ['128', '55.1837748899', '1'],
       ['130', '55.9167038553', '1'],
       ['130', '56.2727376481', '1'],
       ['130', '57.480058071', '1'],
       ['130', '60.3922465138', '1'],
       ['130', '61.2506277637', '1'],
       ['130', '60.5279054759', '1'],
       ['143', '62.139526711', '1'],
       ['143', '65.4147315349', '1'],
       ['143', '72.3278873965', '1'],

and all the values are string as you see. I need to convert them in float values, there's a way to do it? I've found a solution to convert a single string but it doesn't work for the array.

2
  • That looks like ndarray, call astype(float) on it. Commented Jul 29, 2015 at 8:39
  • I added the numpy tag. Commented Jul 29, 2015 at 8:50

2 Answers 2

4

You can use map in a list comprehension :

>>> li=[['120', '29.9475077984', '1'],
...        ['125', '31.3887667742', '1'],
...        ['125', '32.3881706091', '1'],
...        ['125', '34.4894481007', '1'],
...        ['126', '36.1494551046', '1'],
...        ['127', '39.3121447948', '1'],
...        ['128', '43.8203811171', '1'],
...        ['128', '49.3179066095', '1'],
...        ['128', '53.4929489926', '1'],
...        ['128', '55.1837748899', '1'],
...        ['130', '55.9167038553', '1'],
...        ['130', '56.2727376481', '1'],
...        ['130', '57.480058071', '1'],
...        ['130', '60.3922465138', '1'],
...        ['130', '61.2506277637', '1'],
...        ['130', '60.5279054759', '1'],
...        ['143', '62.139526711', '1'],
...        ['143', '65.4147315349', '1'],
...        ['143', '72.3278873965', '1']]
>>> 
>>> 
>>> [map(float,i) for i in li]
[[120.0, 29.9475077984, 1.0], [125.0, 31.3887667742, 1.0], [125.0, 32.3881706091, 1.0], [125.0, 34.4894481007, 1.0], [126.0, 36.1494551046, 1.0], [127.0, 39.3121447948, 1.0], [128.0, 43.8203811171, 1.0], [128.0, 49.3179066095, 1.0], [128.0, 53.4929489926, 1.0], [128.0, 55.1837748899, 1.0], [130.0, 55.9167038553, 1.0], [130.0, 56.2727376481, 1.0], [130.0, 57.480058071, 1.0], [130.0, 60.3922465138, 1.0], [130.0, 61.2506277637, 1.0], [130.0, 60.5279054759, 1.0], [143.0, 62.139526711, 1.0], [143.0, 65.4147315349, 1.0], [143.0, 72.3278873965, 1.0]]

And if you have a numpy array you can use np.astype :

>>> li.astype(float)
array([[ 120.        ,   29.9475078 ,    1.        ],
       [ 125.        ,   31.38876677,    1.        ],
       [ 125.        ,   32.38817061,    1.        ],
       [ 125.        ,   34.4894481 ,    1.        ],
       [ 126.        ,   36.1494551 ,    1.        ],
       [ 127.        ,   39.31214479,    1.        ],
       [ 128.        ,   43.82038112,    1.        ],
       [ 128.        ,   49.31790661,    1.        ],
       [ 128.        ,   53.49294899,    1.        ],
       [ 128.        ,   55.18377489,    1.        ],
       [ 130.        ,   55.91670386,    1.        ],
       [ 130.        ,   56.27273765,    1.        ],
       [ 130.        ,   57.48005807,    1.        ],
       [ 130.        ,   60.39224651,    1.        ],
       [ 130.        ,   61.25062776,    1.        ],
       [ 130.        ,   60.52790548,    1.        ],
       [ 143.        ,   62.13952671,    1.        ],
       [ 143.        ,   65.41473153,    1.        ],
       [ 143.        ,   72.3278874 ,    1.        ]])
Sign up to request clarification or add additional context in comments.

Comments

1

You can use np.ndarray.astype() function -

Example -

In [5]: n= np.array([['120', '29.9475077984', '1'],
   ...:        ['125', '31.3887667742', '1'],
   ...:        ['125', '32.3881706091', '1'],
   ...:        ['125', '34.4894481007', '1'],
   ...:        ['126', '36.1494551046', '1'],
   ...:        ['127', '39.3121447948', '1'],
   ...:        ['128', '43.8203811171', '1'],
   ...:        ['128', '49.3179066095', '1'],
   ...:        ['128', '53.4929489926', '1'],
   ...:        ['128', '55.1837748899', '1'],
   ...:        ['130', '55.9167038553', '1'],
   ...:        ['130', '56.2727376481', '1'],
   ...:        ['130', '57.480058071', '1'],
   ...:        ['130', '60.3922465138', '1'],
   ...:        ['130', '61.2506277637', '1'],
   ...:        ['130', '60.5279054759', '1'],
   ...:        ['143', '62.139526711', '1'],
   ...:        ['143', '65.4147315349', '1'],
   ...:        ['143', '72.3278873965', '1']])

In [8]: n = n.astype(np.float)

In [9]: n
Out[9]:
array([[ 120.        ,   29.9475078 ,    1.        ],
       [ 125.        ,   31.38876677,    1.        ],
       [ 125.        ,   32.38817061,    1.        ],
       [ 125.        ,   34.4894481 ,    1.        ],
       [ 126.        ,   36.1494551 ,    1.        ],
       [ 127.        ,   39.31214479,    1.        ],
       [ 128.        ,   43.82038112,    1.        ],
       [ 128.        ,   49.31790661,    1.        ],
       [ 128.        ,   53.49294899,    1.        ],
       [ 128.        ,   55.18377489,    1.        ],
       [ 130.        ,   55.91670386,    1.        ],
       [ 130.        ,   56.27273765,    1.        ],
       [ 130.        ,   57.48005807,    1.        ],
       [ 130.        ,   60.39224651,    1.        ],
       [ 130.        ,   61.25062776,    1.        ],
       [ 130.        ,   60.52790548,    1.        ],
       [ 143.        ,   62.13952671,    1.        ],
       [ 143.        ,   65.41473153,    1.        ],
       [ 143.        ,   72.3278874 ,    1.        ]])

2 Comments

Thanks for answering. I've just tried your method but it return this: ValueError Traceback (most recent call last) <ipython-input-31-e784ab436424> in <module>() ----> 1 n=n.astype(np.float) ValueError: could not convert string to float:
You have some string that is not convertible to float? Check your array to see if there are any strings that cannot be converted to string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.