7

I need to remove some rows from a 3D numpy array. For exampe:

a = [[1,2,3]
     [4,5,6]
     [7,8,9]

     [9,8,7]
     [6,5,4]
     [3,2,1]]

and I want to remove the third row of both pages of the matrix in order to obtain something like:

 a = [[1,2,3]
     [4,5,6]

     [9,8,7]
     [6,5,4]]

I have tried with

   a = numpy.delete(a, 2, axis=0)

but I can't obtain what I need.

2 Answers 2

10

axis should 1.

>>> import numpy
>>> a = [[[1,2,3],
...       [4,5,6],
...       [7,8,9]],
...      [[9,8,7],
...       [6,5,4],
...       [3,2,1]]]
>>> numpy.delete(a, 2, axis=1)
array([[[1, 2, 3],
        [4, 5, 6]],

       [[9, 8, 7],
        [6, 5, 4]]])
Sign up to request clarification or add additional context in comments.

Comments

0

A different approach would be to use slice notation, where you remove the last row of the 2nd axis.

>>> a[:, :-1, :]
array([[[1, 2, 3],
        [4, 5, 6]],

       [[9, 8, 7],
        [6, 5, 4]]])

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.