I have a 2d numpy array of ints called M. I would like to convert all the ints to strings returning a 2d numpy array of the same shape. How can you do that?
I tried map(str, M)
but that doesn't work as it converts the individual rows into strings.
You can use astype to cast the array to particular type
m = np.array([[1,2,3], [4,5,6]])
m = m.astype(str)
m
array([['1', '2', '3'],
['4', '5', '6']], dtype='<U21')
If I have understood it right, you are trying to convert all integer elements in the array to a string. To do so, this should work:
np.char.mod('%d',M)
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.