Last updated: Apr 12, 2024
Reading time·4 min
To iterate over the columns of a NumPy array:
numpy.transpose()
method or the T
attribute to transpose the axes
of the array.for
loop to iterate over the transposed array.import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) for column in arr.T: print(column) print('-' * 50)
Running the code sample produces the following output.
[1 2 3] -------------------------------------------------- [3 4 5] -------------------------------------------------- [5 6 7] -------------------------------------------------- [7 8 9] --------------------------------------------------
The ndarray.T attribute returns a view of the transposed array.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) # [[1 3 5 7] # [2 4 6 8] # [3 5 7 9]] print(arr) print('-' * 50) # [[1 2 3] # [3 4 5] # [5 6 7] # [7 8 9]] print(arr.T)
In other words, the T
attribute returns the array with its axes permuted.
Using the array.T
attribute is equivalent to calling the
transpose()
method on the array.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) for column in arr.transpose(): print(column) print('-' * 50)
Running the code sample produces the following output.
[1 2 3] -------------------------------------------------- [3 4 5] -------------------------------------------------- [5 6 7] -------------------------------------------------- [7 8 9] --------------------------------------------------
If you need to iterate over the columns of a three-dimensional array, use the following code sample instead.
import numpy as np arr = np.array([[[1, 3, 5, 7], [2, 4, 6, 8]], [[3, 5, 7, 9], [4, 6, 8, 11]]], dtype=object) print(arr) print('-' * 50) for column in arr.transpose(1, 0, 2): print(column) print('-' * 50)
Running the code sample produces the following output.
[[[1 3 5 7] [2 4 6 8]] [[3 5 7 9] [4 6 8 11]]] -------------------------------------------------- [[1 3 5 7] [3 5 7 9]] -------------------------------------------------- [[2 4 6 8] [4 6 8 11]] --------------------------------------------------
The code sample shifts the second dimension (the columns) to the first dimension to iterate over the columns of the 3D array.
The arguments we passed to the method are the axes.
range()
You can also use the range class to iterate over the columns of a NumPy array.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) for column in range(arr.shape[1]): print(arr[:, column]) print('-' * 50)
Running the code sample produces the following output.
[1 2 3] -------------------------------------------------- [3 4 5] -------------------------------------------------- [5 6 7] -------------------------------------------------- [7 8 9] --------------------------------------------------
The range class is commonly used for looping a specific number of times in for loops.
The array.shape() method returns a tuple containing the array's dimensions.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) # (3, 4) print(arr.shape)
The array in the example has 3 rows and 4 columns.
We accessed the tuple at index 1
to pass the number of columns to the
range()
class.
The range()
class created an iterator starting at 0
and going up to, but not
including the column count.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) # [0, 1, 2, 3] print(list(range(arr.shape[1])))
The last step is to access the values in each column using bracket notation.
for column in range(arr.shape[1]): print(arr[:, column])
zip()
You can also use the zip()
function to iterate over the columns of a NumPy
array.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) for column in zip(*arr): print(list(column))
Running the code sample produces the following output.
[1, 2, 3] [3, 4, 5] [5, 6, 7] [7, 8, 9]
The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.
The zip
function returns an iterator of tuples.
import numpy as np arr = np.array([ [1, 3, 5, 7], [2, 4, 6, 8], [3, 5, 7, 9], ]) # [(1, 2, 3), (3, 4, 5), (5, 6, 7), (7, 8, 9)] print(list(zip(*arr)))
We used the
iterable unpacking *
operator to unpack the list of tuples in the call to zip()
.
The last step is to use a for
loop to iterate over the columns of the array.
You can learn more about the related topics by checking out the following tutorials: