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

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** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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.

- Create a 2D Numpy array (skip this step if you already have an array to operate on).
- 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]]`

.

- If you want the values in the last column as a simple one-dimensional array, use the syntax

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.**