How do I access the ith column of a NumPy multidimensional array?

You can access the ith column of a NumPy multidimensional array by using the following syntax:

import numpy as np

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

i = 1
# Access the ith column (0-indexed)
ith_column = arr[:, i]

print(ith_column)

In this example, i is the index of the column you want to access, and arr is the 2D array. The : before the comma indicates that you want to select all rows, and the i after the comma indicates that you want to select the ith column.

Watch a course Python - The Practical Guide

You can also do the same operation using .T(transpose) and indexing

# Access the ith column (0-indexed)
ith_column = arr.T[i]

print(ith_column)

In this example, i is the index of the column you want to access, and arr is the 2D array. Using .T on the 2d array will make the array transpose and the ith column will become ith row which can be accessed via indexing.