I have a list of numbers in string format. I converted that list into numpy array using np.asarray()
.
How do I convert the string elements to ints?
I have a list of numbers in string format. I converted that list into numpy array using np.asarray()
.
How do I convert the string elements to ints?
If you have x = np.matrix
, where each element is '1.0' (as str
) and you want to convert it to int
or float
:
x = x.astype(np.float)
import numpy as np
nums_str = ['1','23','345']
nums_str_np = np.asarray(nums_str)
nums_int_np = nums_str_np.astype('int')
nums_int_np
- is now np array of integers.