How to iterate over the Columns of a NumPy Array

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to iterate over the Columns of a NumPy Array
  2. Iterating over the columns of a 3D NumPy array
  3. Iterating over the Columns of a NumPy Array with range()
  4. Iterate over the Columns of a NumPy Array using zip()

# 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
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)
The code for this article is available on GitHub

Running the code sample produces the following output.

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

iterate over columns of numpy array

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

main.py
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
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
[1 2 3] -------------------------------------------------- [3 4 5] -------------------------------------------------- [5 6 7] -------------------------------------------------- [7 8 9] --------------------------------------------------

iterate over columns of array using transpose method

# 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
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)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[[[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]] --------------------------------------------------

iterating over the columns of 3d numpy array

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
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)
The code for this article is available on GitHub

Running the code sample produces the following output.

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

iterate over columns of numpy array with range

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
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
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
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
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))
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[1, 2, 3] [3, 4, 5] [5, 6, 7] [7, 8, 9]

iterate over columns of array using zip

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
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.