Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deleting first and last rows of an array in Python

How do I delete first and last row of an array in Python? The desired output is attached.

import numpy as np
A=np.array([[0, 1, 0, 1, 1, 0],
       [1, 0, 0, 1, 0, 0],
       [0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 1, 0],
       [1, 0, 1, 1, 0, 1],
       [0, 0, 1, 0, 1, 0]])

Desired output:

array([[1, 0, 0, 1, 0, 0],
       [0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 1, 0],
       [1, 0, 1, 1, 0, 1]])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What about slicing

>>> A[1:-1, :]
array([[1, 0, 0, 1, 0, 0],
       [0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 1, 0],
       [1, 0, 1, 1, 0, 1]])

or using numpy.delete

>>> np.delete(A, [0, -1], axis=0)
array([[1, 0, 0, 1, 0, 0],
       [0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 1, 0],
       [1, 0, 1, 1, 0, 1]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading