Numpy how to iterate over columns of array?

155,562

Solution 1

Just iterate over the transposed of your array:

for column in array.T:
   some_function(column)

Solution 2

This should give you a start

>>> for col in range(arr.shape[1]):
    some_function(arr[:,col])


[1 2 3 4]
[99 14 12 43]
[2 5 7 1]

Solution 3

For a three dimensional array you could try:

for c in array.transpose(1, 0, 2):
    do_stuff(c)

See the docs on how array.transpose works. Basically you are specifying which dimension to shift. In this case we are shifting the second dimension (e.g. columns) to the first dimension.

Solution 4

You can also use unzip to iterate through the columns

for col in zip(*array):
   some_function(col)

Solution 5

for c in np.hsplit(array, array.shape[1]):
    some_fun(c)
Share:
155,562
User
Author by

User

Updated on December 27, 2021

Comments

  • User
    User over 2 years

    Suppose I have and m x n array. I want to pass each column of this array to a function to perform some operation on the entire column. How do I iterate over the columns of the array?

    For example, I have a 4 x 3 array like

    1  99 2
    2  14 5
    3  12 7
    4  43 1
    
    for column in array:
      some_function(column)
    

    where column would be "1,2,3,4" in the first iteration, "99,14,12,43" in the second, and "2,5,7,1" in the third.

  • Ibrahim Muhammad
    Ibrahim Muhammad over 10 years
    What would be a good way to combine the result back into a single array?
  • gronostaj
    gronostaj about 10 years
    It doesn't look pythonic to me.
  • drevicko
    drevicko over 9 years
    For those wondering, array.T isn't costly, as it just changes the 'strides' of array (see this answer for an interesting discussion)
  • Neil G
    Neil G about 6 years
    @gronostaj Of course it's Pythonic. How else would you solve this problem when you want to iterate over an arbitrary axis of a multidimensional array?
  • gronostaj
    gronostaj about 6 years
    @NeilG This question is strictly about 2-dimensional arrays.
  • Rufus
    Rufus over 3 years
    Is there a way of iterating which keeps the vectors as column vectors?
  • Bill
    Bill over 3 years
    Interesting. This returns tuples instead of arrays. And it's much faster.
  • Ela782
    Ela782 about 3 years
    I have a hunch that the result of this might depend on the storage order of the numpy array ('C' or 'F') - it may return columns in one case and rows in the other. I'm not sure though - just a warning, better check before using this. It doesn't look safe.
  • rv.kvetch
    rv.kvetch over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.