How to select last N rows of NumPy array

In this post, we are going to learn how to select last N rows of NumPy array with examples. To select the last N rows from the NumPy array negative is used. We will cover getting the last row by using negative indexing and selecting the last N row by using the np.r_ () function, when we use the negative index in the slicing of the NumPy array is called negative indexing. get the last element of the NumPy array. To run all these programs make sure the NumPy library is Installed on the local machine.

1. How to select Last N rows of NumPy array


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

ndarray[rowindex]

To select multiple rows by index we use this syntax

Syntax

ndarray[Startindex : EndIndex, : ]

Parameters

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

In this example, we are Selecting the last row from the NumPy array, and all columns of the last row by using code origanlArr[-1, :].This is how to get the last row of the NumPy array using negative indexing

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print('select first firve rows: \n',origanlArr[-1, :])

Output

select Last row from NumPy Array: 
 [[10 20 30 40 50 60 70]]

2. How to select last 5 rows from NumPy array


In this python program example, We are selecting the last 5 rows of the NumPy array by using negative indexing with slicing and all columns of the selected 5 rows by using a colon(:).This can work for 2D and Multidimensional array.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print('select Last 5 rows of NumPy Array: \n',origanlArr[-5:, :])

Output

select Last 5 rows of NumPy Array 
[[ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]
 [10 20 30 40 50 60 70]]

3. How to select last 2 rows of NumPy array


In this python Example, we will understand, how to select last N rows of NumPy array by selecting the last 2 rows of the NumPy array by using the negative index in slicing and all columns of selected rows by using a colon(:).

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print('select Last 2 rows of  NumPy Array: \n',origanlArr[-2:, :])

Output

select Last 2 rows of  NumPy Array: 
 [[ 9 18 27 36 45 54 63]
 [10 20 30 40 50 60 70]]

4. How to select last 6 rows from NumPy array


Another way to select the last n rows of the NumPy array is by using np.r_ () function that Translates slice objects to concatenation along the first axis. If we use slicing notation[start:stop: step] it works like the np.arange() function. It will select the last 6 rows from the given NumPy array.

import numpy as np

origanlArr = np.array ([[3,6,9,12,15,18,21],
 [7,14,21,28,35,42,49],
 [11,22,33,44,55,66,77],
[12,24,36,48,60,72,84],
[ 9, 18, 27, 36,45,54,63],
[ 10, 20, 30, 40,50,60,70]])

print('select Last 6 rows of  NumPy Array: \n',origanlArr[np.r_[6:],:])

Output

select Last 6 rows of  NumPy Array: 
 [[ 3  6  9 12 15 18 21]
 [ 7 14 21 28 35 42 49]
 [11 22 33 44 55 66 77]
 [12 24 36 48 60 72 84]
 [ 9 18 27 36 45 54 63]
 [10 20 30 40 50 60 70]]

Summary

In this post, we have learned How to select last N rows of NumPy array by using negative index slicing and NumPy library function np.r_() with examples