In this article we will discuss how to select elements from a 2D Numpy Array . Elements to select can be a an element only or single/multiple rows & columns or an another sub 2D array.

First of all, let’s import numpy module i.e.

Now let’s create a 2d Numpy Array by passing a list of lists to numpy.array() i.e.

Contents of the 2D Numpy Array will be,

Now let’s see how to select elements from this 2D Numpy Array by index i.e.

Select a single element from 2D Numpy Array by index

We can use [][] operator to select an element from Numpy Array i.e.

Example 1:

Select the element at row index 1 and column index 2.

Output:

Example 2:

Or we can pass the comma separated list of indices representing row index & column index too i.e.

Output:

Select Rows by Index from a 2D Numpy Array

We can call [] operator to select a single or multiple row. To select a single row use,

It will return a complete row at given index.

To select multiple rows use,

It will return rows from start_index to end_index – 1 and will include all columns.

Let’s use this,

Contents of the 2D a Numpy Array nArr2D created above are,

Let’s select a row at index 2 i.e.

Output:

Select multiple rows from index 1 to 2 i.e.

Output:

Select multiple rows from index 1 to last index

Output:

Select Columns by Index from a 2D Numpy Array

To select a single column use,

It will return a complete column at given index.

To select multiple columns use,

It will return columns from start_index to end_index – 1.

Let’s use these,

Contents of the 2D Numpy Array nArr2D created above are,

Select a column at index 1

Output:

Select multiple columns from index 1 to 2

Output:

Select multiple columns from index 1 to last index

Output is same as above because there are only 3 columns 0,1,2. So 1 to last columns means columns at index 1 & 2.

Select a Sub Matrix or 2d Numpy Array from another 2D Numpy Array

To select sub 2d Numpy Array we can pass the row & column index range in [] operator i.e.

It will return a sub 2D Numpy Array for given row and column range.

Let’s use these,

Contents of the 2D Numpy Array nArr2D created at start of article are,

Select a sub 2D Numpy Array from row indices 1 to 2 & column indices 1 to 2

Output:

Selected Row or Column or Sub Array is View only

Contents of the Numpy Array selected using [] operator returns a View only i.e. any modification in returned sub array will be reflected in original Numpy Array .
Let’s check this,

Contents of the 2D Numpy Array nArr2D created at start are,

Select a row at index 1 from 2D array i.e.

Contents of row : 

Now modify the contents of row i.e.

New contents of the row will be

Modification in sub array will be reflected in main Numpy Array too. Updated Contents of the 2D Numpy Array nArr2D are,

Get a copy of 2D Sub Array from 2D Numpy Array using ndarray.copy()

to the copy instead of view in sub array use copy() function.
Let’s check this,

Create a 2D Numpy adArray with3 rows & columns | Matrix

Content of nArr2D is,

Select a copy of row at index 1 from 2D array and set all the elements in selected sub array to 100

Here, sub array is a copy of original array so, modifying it will not affect the original Numpy Array
Contents of the modified sub array row is,

Contents of the original Numpy Array is,

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.