3

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?

iacob
  • 14,010
  • 5
  • 54
  • 92
Uddesh Jain
  • 866
  • 1
  • 13
  • 14

2 Answers2

2

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)
Adrian Mole
  • 43,040
  • 110
  • 45
  • 72
2
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.

thepunitsingh
  • 655
  • 1
  • 12
  • 28