0

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.

2 Answers 2

1

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')
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.