2

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]]
8
  • Since you want to remove a column, try MyArray[:,1:] Commented Mar 4, 2022 at 10:43
  • please enter array variable in python, to copy on IDE and test Commented Mar 4, 2022 at 10:52
  • Why aren't there commas in the array ? 'Cause I can't test it without them Commented Mar 4, 2022 at 10:52
  • 1
    @StijnB: There are no commas, because it is the way numpy arrays are printed. But you are right on one point this questions lacks a true minimal reproducible example. Commented Mar 4, 2022 at 10:55
  • @SergeBallesta Was editing My post, Hope its better like that Commented Mar 4, 2022 at 10:56

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

2

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

1 Comment

Well this will give a Python array of numpy arrays... Not wrong but probably not what is expected here.
1

It should be straight forward with

new_array = MyArray[:, 1:]

See this link for explanation and examples. Or this link

Comments

0

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

1 Comment

Same issue than with np.delete(MyArray, 0, 1)

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.