Skip to Content

Get the Last N Rows of a 2D Numpy Array

The Numpy library in Python comes with a number of useful methods and techniques to work with and manipulate data in arrays. In this tutorial, we will look at how to get the last N rows of a two-dimensional Numpy Array with the help of some examples.

How to get the last n rows of a 2D Numpy array?

get the last n rows of a 2d numpy array

You can use slicing to get the last N rows of a 2D array in Numpy. Here, we use row indices to specify the range of rows that we’d like to slice. To get the last n rows, use the following slicing syntax –

# last n rows of numpy array
ar[-n:, :]

It returns the last n rows (including all the columns) of the given array.

Steps to get the last n rows of 2D array

Let’s now look at a step-by-step example of using the above syntax on a 2D Numpy array.

Step 1 – Create a 2D Numpy array

First, we will create a 2D Numpy array that we’ll operate on.

import numpy as np

# create a 2D array
ar = np.array([
    ['Tim', 181, 86],
    ['Peter', 170, 68],
    ['Isha', 158, 59],
    ['Rohan', 168, 81],
    ['Yuri', 171, 65],
    ['Emma', 166, 64],
    ['Michael', 175, 78],
    ['Jim', 190, 87],
    ['Pam', 168, 57],
    ['Dwight', 187, 84]
])
# display the array
print(ar)

Output:

[['Tim' '181' '86']
 ['Peter' '170' '68']
 ['Isha' '158' '59']
 ['Rohan' '168' '81']
 ['Yuri' '171' '65']
 ['Emma' '166' '64']
 ['Michael' '175' '78']
 ['Jim' '190' '87']
 ['Pam' '168' '57']
 ['Dwight' '187' '84']]

Here, we used the numpy.array() function to create a 2D Numpy array with 10 rows and 3 columns. The array contains information on the height (in cm) and weight (in kg) of some employees in an office.

Step 2 – Slice the array to get the last n rows

To get the last n rows of the above array, slice the array starting from the nth last row up to the end of the array. You can use negative indexing to get the index of the nth row from the end (which will be -n).

For example, you can use -5 as the index of the 5th row from the end.

For example, let’s get the last 5 rows from the array that we created in step 1.

# last 5 rows
print(ar[-5:, :])

Output:

[['Emma' '166' '64']
 ['Michael' '175' '78']
 ['Jim' '190' '87']
 ['Pam' '168' '57']
 ['Dwight' '187' '84']]

We get the last 5 rows of the 2D array.

For more on slicing Numpy arrays, refer to its documentation.

Summary

In this tutorial, we looked at how to get the last n rows of a two-dimensional Numpy array using slicing. The following is a short summary of the steps mentioned in the tutorial.

  1. Create a 2D Numpy array (skip this step if you already have an array to operate on).
  2. Slice the array from the nth last row up to the end of the array. Using a negative index can be helpful here.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.