简体   繁体   中英

How to convert numpy array elements from string to int

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?

如果您有x = np.matrix ,其中每个元素都是 '1.0' (作为str )并且您想将其转换为intfloat

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM