How to select rows from NumPy array

In this post, we are going to learn how to select rows from NumPy array 2D or 3D with examples. We will use slicing or Ellipsis or np. r_[] method.We will cover selecting multiple rows from the NumPy array, selecting multiple rows by index, Selecting the last row from the NumPy array, and selecting a range of rows from the nummy array.

1. How to select rows from NumPy array 2D using slicing


The syntax to select the rows using slicing is given below, where the colon(:) represents that we are selecting all columns and passing row index or position to select the correspondence rows.

Syntax

numpyarry[ row  ,:] 

Parameters

  • Row: The row represents the row index or position for whatever column we have to select.
  • Colon(:) : We are selecting all columns.

How does it work

  • We have created a NumPy array of size 12 and distributed it into 3 rows and 4 columns.
  • Selecting the first row by using [0,:]
  • Selecting the 2nd row by using [1,:]
  • Selecting the 3rd row by using [2, :]

Python program to select rows from NumPy array

import numpy as np

origanlArr = np.array ([[ 0 , 1 , 2 , 3],
 [ 4,  5 , 6 , 7],
 [ 8 , 9, 10, 11]])



Frist_row = origanlArr[0, :]
Sec_Row = origanlArr[1, :]
Thir_row = origanlArr[2, :]

print('\nFirst Row:',Frist_row )
print('Second Row:',Sec_Row)
print('Third Row:',Thir_row)



Output

First Row: [0 1 2 3]
Second Row: [4 5 6 7]
Third Row: [ 8  9 10 11]

2. NumPy Select multiple rows by index


We use Index brackets ([]) to select rows from the NumPy array. We can select single or multiple rows using this syntax.To select a single row by index we use this syntax.

ndarray[rowindex]

To select multiple rows by index we use this syntax

ndarray[Startindex : EndIndex, : ]
  • StartIndex : EndIndex: It will select the row start from startindex till to EndIndex-1.
  • colon(:) : It will select all columns of the given array
import numpy as np

origanlArr = np.array ([[ 0 , 1 , 2 , 3],
 [ 4,  5 , 6 , 7],
 [ 8 , 9, 10, 11]])

rowsbyIndex = origanlArr[1:3, :]

print('\nSelected rows by Index:\n',rowsbyIndex )


Output

Selected rows by Index:
 [[ 4  5  6  7]
 [ 8  9 10 11]]

3. How to select rows from NumPy array 3D


In this Python example, We will learn how to select a specific row from a 3D NumPy array of shape(x,y,z). We are selecting the rows from the 3D NumPy array using np3Darr[0,:, 1]

np3Darr = np.array([[[15, 16, 17, 18],
                                [3, 6, 9, 12]],
                              [[4, 4, 12, 20],
                             [6, 12, 18, 24]]])

row = np3Darr[0, :, 1]

print('selected row:\n',row)

Output

selected row:
 [16  6]

4. Using Ellipsis to select rows from NumPy array


The syntax to select the column using slicing is given below, where the colon(:) represents that we are selecting all columns and passing row index or position to select the correspondence rows.

Syntax

numpyarry[ row  ,:] 

Parameters

  • Row: The row represents the row index or position for whatever column we have to select.
  • Colon(:) : We are selecting all columns.

How does it work

  • We have created a NumPy array of size 12 and distributed it into 3 rows and 4 columns.
  • Selecting the first row by using [0,:]
  • Selecting the 2nd row by using [1,:]
  • Selecting the 3rd row by using [2, :]
import numpy as np

origanlArr = np.arange(12).reshape(3,4)

print('Original array:\n',origanlArr)



Frist_row = origanlArr[0, ...]
Sec_Row = origanlArr[1, ...]
Thir_row = origanlArr[2, ...]

print('\nFirst Row:',Frist_row )
print('Second Row:',Sec_Row)
print('Third Row:',Thir_row)


Output

Original array:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

First Row: [0 1 2 3]
Second Row: [4 5 6 7]
Third Row: [ 8  9 10 11]

5. np_r[] tp select range of rows from numpy array


We can select the range of rows by using the np.r_ () function Translates slice objects to concatenation along the first axis. If we use slicing notation[start:stop:step] it works like the np.arange() function.

import numpy as np

origanlArr = np.array ([[ 0 , 1 , 2 , 3],
 [ 4,  5 , 6 , 7],
 [ 8 , 9, 10, 11]])



print('\n selected range of rows:\n',origanlArr[ np.r_[0:1, 2],:])


Output

 selected range of rows:
 [[ 0  1  2  3]
 [ 8  9 10 11]]

6. Select the last row from the NumPy array


In this example, we are selecting the last row of the NumPy array using slicing. In this code origanlArr[-1,:] -1 represents the last row of the NumPy array, and colon(:) is used to select all columns of the last row.

import numpy as np

origanlArr = np.array ([[ 0 , 1 , 2 , 3],
 [ 4,  5 , 6 , 7],
 [ 8 , 9, 10, 11]])

print('\n selected last row from Numpy array:\n',origanlArr[-1 ,:])


Output

 selected last row from Numpy array:
 [ 8  9 10 11]