Get ith Column from 2D NumPy Array in Python

In this article, we will learn how to access the ith column of a 2D NumPy Array in Python.

Table Of Contents

Suppose we have a NumPy Array,

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

We want to access the 3rd column from this 2D NumPy Array. As indexing starts from 0, so the index position of third column is 2. Contents of the column at index position 2 should be selected as a NumPy Array i.e.

[2 7 2 7]

There are multiple ways to access the ith column of a 2D NumPy array. Lets discuss all the methods one by one with proper approach and a working code example

Select ith Column of a NumPy Array using Slicing.

Slicing in python can be defined as taking elements from one given index to another given index.

Example:

arr = [[1,2,3],
       [2,3,4]]

arr[:, 0]   ===> This will give the first column [1,2]
arr[:, 1]   ===> This will give the second column [1,2]
arr[:, 1:]  ===> This will give the all the columns starting from 2nd column i.e. [[2, 3], [3, 4]]

Approach to access the ith column from a NumPy Array:

  1. Import numpy library and create a numpy array .
  2. Pass the index of the column to be accessed as slicing index.
  3. Print the array returned after slicing.

Source Code

import numpy as np

# creating a numpy array
arr = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
                [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]])

# The ith index
i = 2

# Access the ith column of a 2D NumPy array
column_i = arr[:,i]

# Printing the column
print(column_i)

Output:

[2 7 2 7]

We selected the column at index position 2 and printed it.

Select ith Column of a NumPy Array using Transpose

Transposing of an array will interchange the rows with columns and columns with rows i.e, columns will become rows. Now we can access the ith row to get the ith column.

Rows in a ndarray can be accessed using indexing.

Example:

arr = [[1,2],
       [2,3]
       [3,4]]

transposedArr = [[1,2,3],
                 [2,3,4]]

transposedArr[0]  ===> This will give the first column from original array [1,2,3]
transposedArr[1]  ===> This will give the second column from original array [2,3,4]

Approach:

  1. Import numpy library and create numpy array .
  2. Transpose the given array using the .T property.
  3. Pass the ith index as slicing index.
  4. Print the array returned after slicing.

Source Code

import numpy as np

# creating a numpy array
arr = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
                [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]])

# ith index
i = 2

# Access the ith column of a 2D NumPy array
column_i = arr.T[i]

#printing the column
print(column_i)

Output:

[2 7 2 7]

We selected the column at index position 2 and printed it.

Select ith Column of a NumPy Array using Ellipsis (…)

Ellipsis is a singleton Object and It has no Methods. The ellipsis can be used for Accessing and slicing multidimensional Arrays.

Example:

arr = [[1,2,3],
       [2,3,4]]

arr[...,  0]  ===> This will give the first row [1,2]
arr[... , 1]  ===> This will give the second column [2,3]

An index can only have single ellipsis, i.e, a[… , …] is not allowed

Approach:

  1. Import numpy library and create numpy array .
  2. Pass the ith index along with the ellipsis.
  3. Print the returned column.

Source Code

import numpy as np

# Creating a NumPy Array
arr = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
                [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]])

# ith index
i = 2

# Access the ith column of a 2D NumPy array
column_i = arr[..., i]

# Printing the column
print(column_i)

Output:

  [2 7 2 7]

Select ith Column of a NumPy Array using List comprehension and indexing

Iterate over the all the rows of given array, for each row, access the ith element in the row.

Accesing the elements in a 1d array.

Example:

arr = [ 1, 2, 3, 4, 5 ]

arr[0]  ===> this will give the first element 1
arr[3]  ===> this will give the fourth element 4

Approach:

  1. Import numpy library and create numpy array .
  2. Iterate over the array
  3. Access the ith element of the row and append it to a list
  4. Print the list.

Source Code

import numpy as np

# creating a numpy array
arr = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ],
                [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
                [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]])

# ith index
i = 2

# Access the ith column of a 2D NumPy array
column_i = [row[i] for row in arr]

# Printing the column
print(column_i)

Output:

[2, 7, 2, 7]

It selected the column at index position 2 from the NumPy Array.

Summary

Great! you made it, we have discussed all possible methods to access the ith column of a 2D NumPy array. Happy learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top