5

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
  • 20,084
  • 6
  • 92
  • 119
Uddesh Jain
  • 1,064
  • 2
  • 15
  • 16

2 Answers2

3

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
  • 49,934
  • 160
  • 51
  • 83
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
  • 713
  • 1
  • 12
  • 30