2

How can I convert the second column of this matrix into int type inplace?

import numpy as np
x = np.array([[1.23e+02, 2.3], [1.3e+01, 2.9],[1.2e+01, 83.3]])

Desired output:

array([[ 123. ,    2],
       [  13. ,    2],
       [  12. ,   83]])

The best I can come out with is this, but not inlace

x[:,1].astype(int)
3
  • 1
    You can't change the typing of the array in-place (unless I'm grossly mistaken), but you can floor. Commented Nov 27, 2015 at 5:44
  • Only one dtype per array, either all int or all float. (or all object to allow both int and float, but that's idiotic: you'll loose all of numpy numerical performance...) Commented Nov 27, 2015 at 5:50
  • Why do you need to convert it, and why does it need to be 'inplace'? Commented Nov 27, 2015 at 7:01

1 Answer 1

2

You can't change the typing of the array in-place, but you can truncate (or floor if you prefer):

>>> import numpy as np
>>> x = np.array([[1.23e+02, 2.3], [1.3e+01, 2.9],[1.2e+01, 83.3]])
>>> np.trunc(x[:,1], x[:,1])
array([  2.,   2.,  83.])
>>> x
array([[ 123.,    2.],
       [  13.,    2.],
       [  12.,   83.]])
Sign up to request clarification or add additional context in comments.

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.