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:
 Import numpy library and create a numpy array .
 Pass the index of the column to be accessed as slicing index.
 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:
 Import numpy library and create numpy array .
 Transpose the given array using the .T property.
 Pass the ith index as slicing index.
 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:
 Import numpy library and create numpy array .
 Pass the ith index along with the ellipsis.
 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:
 Import numpy library and create numpy array .
 Iterate over the array
 Access the ith element of the row and append it to a list
 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.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.