184

How to convert

["1.1", "2.2", "3.2"]

to

[1.1, 2.2, 3.2]

in NumPy?

Vivz
  • 6,625
  • 2
  • 17
  • 33
Meh
  • 7,016
  • 10
  • 53
  • 76

5 Answers5

258

Well, if you're reading the data in as a list, just do np.array(map(float, list_of_strings)) (or equivalently, use a list comprehension). (In Python 3, you'll need to call list on the map return value if you use map, since map returns an iterator now.)

However, if it's already a numpy array of strings, there's a better way. Use astype().

import numpy as np
x = np.array(['1.1', '2.2', '3.3'])
y = x.astype(np.float)
user2357112
  • 260,549
  • 28
  • 431
  • 505
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • and if you have a array with an string that i want to maintain? like ['a','1.1','2.2','3.3'] -> ['a',1.1,2.2,3.3] – ePascoal May 09 '15 at 20:09
  • 8
    @MrMartin - Then use a `list`. Numpy arrays are deliberately homogenously typed. If you really want, you can use an object array (e.g. `np.array(['apple', 1.2, 1, {'b'=None, 'c'=object()}], dtype=object)`). However, object arrays don't have any significant advantages over using a list. – Joe Kington May 09 '15 at 20:14
  • ```print(list(y))``` gives ```[1.1, 2.2, 3.3] [Program finished] ``` as requested by OP – Subham May 03 '21 at 10:15
  • Does not work if any of the strings is an empty string (''). – germ Oct 19 '21 at 23:03
20

Another option might be numpy.asarray:

import numpy as np
a = ["1.1", "2.2", "3.2"]
b = np.asarray(a, dtype=float)

print(a, type(a), type(a[0]))
print(b, type(b), type(b[0]))

resulting in:

['1.1', '2.2', '3.2'] <class 'list'> <class 'str'>
[1.1 2.2 3.2] <class 'numpy.ndarray'> <class 'numpy.float64'>
divenex
  • 15,176
  • 9
  • 55
  • 55
Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34
  • 2
    I benchmarked all the answers here in python 2.7. Assuming I'm given a list of 512 strings which represent floating point numbers, this approach was the fastest (slightly faster than pradeep bisht's answer, about 1.5 times faster than Thomio's answer, and more than twice as fast as the accepted answer). Have an upvote! – jodag Aug 13 '18 at 04:47
  • 3
    "order='C'" is not needed here since this is a 1-dim array. This also works (at least in Python 3.6.9): b=np.array(a, dtype=float) – DavidS Sep 21 '20 at 01:36
  • add `.tolist()` at the end, like that `b = np.asarray(d, dtype=np.float64).tolist()` to get comma separated list – Yu Da Chi Nov 23 '20 at 09:27
7

If you have (or create) a single string, you can use np.fromstring:

import numpy as np
x = ["1.1", "2.2", "3.2"]
x = ','.join(x)
x = np.fromstring( x, dtype=np.float, sep=',' )

Note, x = ','.join(x) transforms the x array to string '1.1, 2.2, 3.2'. If you read a line from a txt file, each line will be already a string.

Thomio
  • 1,403
  • 15
  • 19
5

You can use this as well

import numpy as np
x=np.array(['1.1', '2.2', '3.3'])
x=np.asfarray(x,float)
pradeep bisht
  • 66
  • 1
  • 5
5

You can use np.array() with dtype = float:

import numpy as np

x = ["1.1", "2.2", "3.2"]
y = np.array(x,dtype=float)

Output:

array([1.1, 2.2, 3.2])
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
Mayank Soni
  • 81
  • 1
  • 4