Skip to Content

How to Get the First 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 first column of a two-dimensional Numpy array with the help of some examples.

Steps to get the first column of a Numpy Array

get the first column of a numpy array

Let’s look at a step-by-step example of how to extract the first 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 first column of the above array contains the values 1, 4, and 7

Step 2 – Slice the array to get the first column

You can use slicing to extract the first column of a Numpy array. The idea is to slice the original array for all the rows and just the first column (which has a column index of 0).

For example, to get the first column of the array ar use the syntax ar[:, 0].

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

# get the first column
ar[:, 0]

Output:

array([1, 4, 7])

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

ar[:, 0].shape

Output:

(3,)

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

# get the first column
ar[:, [0]]

Output:

array([[1],
       [4],
       [7]])

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

ar[:, [0]].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 first 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 first column of the array –
    • If you want the values in the first column as a simple one-dimensional array, use the syntax ar[:, 0].
    • 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[: [0]].

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