close

How do I extract a sub-array from a numpy 2d array?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I extract a sub-array from a numpy 2d array in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I extract a sub-array from a numpy 2d array?

  1. How do I extract a sub-array from a numpy 2d array?

    Given this array:
    >>> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

  2. extract a sub-array from a numpy 2d array

    Given this array:
    >>> a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

Method 1

Given this array:

>>> a
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

You can slice it along both dimensions:

>>> a[:2,:2]
array([[1, 2],
       [4, 5]])

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read