Home > Software design >  Delete first column of a numpy array
Delete first column of a numpy array

Time:03-04

I have the following np.array():

[[55.3  1.   2.   2.   2.   2. ]
 [55.5  1.   2.   0.   2.   2. ]
 [54.9  2.   2.   2.   2.   2. ]
 [47.9  2.   2.   2.   0.   0. ]
 [57.   1.   2.   2.   0.   2. ]
 [56.6  1.   2.   2.   2.   2. ]
 [54.7  1.   2.   2.   2.   nan]
 [51.4  2.   2.   2.   2.   2. ]
 [55.3  2.   2.   2.   2.   nan]]

And I would Like to get the following one :

[[1.   2.   2.   2.   2. ]
 [1.   2.   0.   2.   2. ]
 [2.   2.   2.   2.   2. ]
 [2.   2.   2.   0.   0. ]
 [1.   2.   2.   0.   2. ]
 [1.   2.   2.   2.   2. ]
 [1.   2.   2.   2.   nan]
 [2.   2.   2.   2.   2. ]
 [2.   2.   2.   2.   nan]]

I did try :

MyArray[1:]#But this delete the first line

np.delete(MyArray, 0, 1) #Where I don't understand the output
[[ 2.  2.  2.  2.  2.]
 [ 1.  2.  2.  2.  2.]
 [ 1.  2.  0.  2.  2.]
 [ 2.  2.  2.  2.  2.]
 [ 2.  2.  2.  0.  0.]
 [ 1.  2.  2.  0.  2.]
 [ 1.  2.  2.  2.  2.]
 [ 1.  2.  2.  2. nan]
 [ 2.  2.  2.  2.  2.]
 [ 2.  2.  2.  2. nan]]

CodePudding user response:

You made a bit of a mistake using np.delete, The np.delete arguments are array,list of indexes to be deleted, axis. By using the below snippet you get the output you want. arr=np.delete(arr,[0],1) The problem you created was, you passed integer instead of a list, which is why it isn't giving correct output.

CodePudding user response:

You could try: new_array = [i[1:] for i in MyArray]

CodePudding user response:

Try MyArray[:,1:] I think you can get rid of column 0 with this

CodePudding user response:

It should be straight forward with

new_array = MyArray[:, 1:]

See this link for explanation and examples. Or this link

  • Related