# How to iterate over the Columns of a NumPy Array

Last updated: Apr 12, 2024
4 min

## #How to iterate over the Columns of a NumPy Array

To iterate over the columns of a NumPy array:

1. Use the `numpy.transpose()` method or the `T` attribute to transpose the axes of the array.
2. Use a `for` loop to iterate over the transposed array.
main.py
```Copied!```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.

shell
```Copied!```[1 2 3]
--------------------------------------------------
[3 4 5]
--------------------------------------------------
[5 6 7]
--------------------------------------------------
[7 8 9]
--------------------------------------------------
``````

The ndarray.T attribute returns a view of the transposed array.

main.py
```Copied!```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.

Transposing the array is very efficient because it changes its strides, the original array is not mutated.

Using the `array.T` attribute is equivalent to calling the transpose() method on the array.

main.py
```Copied!```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.

shell
```Copied!```[1 2 3]
--------------------------------------------------
[3 4 5]
--------------------------------------------------
[5 6 7]
--------------------------------------------------
[7 8 9]
--------------------------------------------------
``````

## #Iterating over the columns of a 3D NumPy array

If you need to iterate over the columns of a three-dimensional array, use the following code sample instead.

main.py
```Copied!```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.

shell
```Copied!```[[[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.

## #Iterating over the Columns of a NumPy Array with `range()`

You can also use the range class to iterate over the columns of a NumPy array.

main.py
```Copied!```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.

shell
```Copied!```[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.

main.py
```Copied!```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.

main.py
```Copied!```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.

main.py
```Copied!```for column in range(arr.shape[1]):
print(arr[:, column])
``````

## #Iterate over the Columns of a NumPy Array using `zip()`

You can also use the `zip()` function to iterate over the columns of a NumPy array.

main.py
```Copied!```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.

shell
```Copied!```[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.

main.py
```Copied!```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.