How to access the ith column of a Python NumPy multidimensional array?

Sometimes, we want to access the ith column of a Python NumPy multidimensional array.

In this article, we’ll look at how to access the ith column of a Python NumPy multidimensional array.

How to access the ith column of a Python NumPy multidimensional array?

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.

For instance, we write:

import numpy

test = numpy.array([[1, 2], [3, 4], [5, 6]])
col = test[:, 1]
print(col)

We create the test NumPy array with a nested list.

Then we get the 2nd column of the NumPy array with test[:, 1] and assign the returned array to col.

Therefore, col is [2 4 6].

Conclusion

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.