get the last column of a 2d numpy array

How to Get the Last Column of a Numpy Array?

Numpy arrays are very versatile when it comes to manipulating and extracting values. In this tutorial, we will look at how to get the last column of a two-dimensional Numpy array with the help of some examples.

Steps to get the last column of a Numpy Array

get the last column of a 2d numpy array

Let’s look at a step-by-step example of how to extract the last column from a two-dimensional Numpy array.

Step 1 – Create a 2D Numpy array

First, we will create a 2D Numpy array that we will use throughout this tutorial.

import numpy as np

# create 2D Numpy array
ar = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])
# display the array
print(ar)

Output:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Here, we used the numpy.array() function to create a two-dimensional Numpy array having three rows and three columns.

You can see that the last column of the above array contains the values 3, 6, and 9.

Step 2 – Slice the array to get the last column

You can use slicing to extract the last column of a Numpy array. The idea is to slice the original array for all the rows and just the last column. Using a negative index can be useful here (the column index of the last column is -1).

For example, to get the last column of the array ar use the syntax ar[:, -1].

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Let’s get the last column of the array created above.

# get the last column
ar[:, -1]

Output:

array([3, 6, 9])

We get a Numpy array of the values in the last column of our original array, ar. Note that the resulting array, here, is one-dimensional.

ar[:, -1].shape

Output:

(3,)

If you instead want the last column as a Numpy array with shape (3, 1) (like a column vector) use the following syntax –

# get the last column
ar[:, [-1]]

Output:

array([[3],
       [6],
       [9]])

We get the last column from the original array as a separate column (sort of like a column vector). Let’s see its shape.

ar[:, [-1]].shape

Output:

(3, 1)

You can see that the resulting array is of shape (3, 1)

Summary

In this tutorial, we looked at how to extract the last column of a two-dimensional Numpy array. The following is a short summary of the steps mentioned in this tutorial.

  1. Create a 2D Numpy array (skip this step if you already have an array to operate on).
  2. Use slicing to get the last column of the array –
    • If you want the values in the last column as a simple one-dimensional array, use the syntax ar[:, -1].
    • If you want the resulting values in a column vector (for example, with shape (n, 1) where n is the total number of rows), use the syntax ar[: [-1]].

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 Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top