How to access a NumPy array by column
NumPy arrays are used to handle large datasets efficiently. When working with 2D arrays, you may need to access specific columns for analysis or processing. NumPy makes this easy with simple indexing methods.

Example:
Given array: 1 13 6
9 4 7
19 16 2
Input: print(NumPy_array_name[:, 2])
Output: [6 7 2]
Explanation: printing 3rd column
Let's explore different ways to access 'i th' column of a 2D array in Python.
Using slicing
Slicing is the easiest and fastest way to access a specific column in a NumPy 2D array using arr[:, column_index], where : selects all rows and column_index picks the desired column.
Syntax:
NumPy_array_name[ : ,column]
import numpy as np
a= [[1, 13, 6], [9, 4, 7], [19, 16, 2]]
b = np.array(a)
print(b[:, 2])
Output
[6 7 2]
Explanation:
- list a is converted into array b.
- b[:, 2] prints the third column (all rows, column index 2).
Using transpose
Transpose the given array using the .T property and pass the index as a slicing index to print the array.
import numpy as np
a = np.array([[1, 13, 6], [9, 4, 7], [19, 16, 2]])
c= a.T[2]
print(c)
Output
[6 7 2]
Explanation:
- a is transposed using .T, swapping rows with columns.
- arr.T[2] accesses the third column as a row and prints it.
Using list comprehension
Here, we access the ith element of the row and append it to a list using the list comprehension and printed the col.
import numpy as np
a = np.array([[1, 13, 6], [9, 4, 7], [19, 16, 2]])
c = [row[1] for row in a]
print(c)
Output:
[13, 4, 16]
Explanation:
- a is converted into array.
- c extracts and prints the second element (index 1) from each row.
Using ellipsis
Ellipsis (...) lets you select rows or columns without writing full slices. It’s useful for accessing data in multi-dimensional arrays.
Syntax:
numpy_Array_name[...,column]
import numpy as np
a = [[1, 13, 6], [9, 4, 7], [19, 16, 2]]
b = np.array(a)
print(b[..., 0])
Output
[ 1 9 19]
Explanation: b[..., 0] prints the first column (index 0) of the array using ellipsis (...) to select all rows.
Related Articles: