144

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.

MERose
  • 3,421
  • 7
  • 48
  • 73
User
  • 56,228
  • 70
  • 174
  • 240
  • 2
    Can't you use an index --- http://stackoverflow.com/questions/4455076/numpy-access-an-array-by-column – ev-br Apr 13 '12 at 21:57

9 Answers9

270

Just iterate over the transposed of your array:

for column in array.T:
   some_function(column)
tillsten
  • 13,650
  • 5
  • 31
  • 40
  • 9
    What would be a good way to combine the result back into a single array? – Ibrahim Muhammad Sep 23 '13 at 17:08
  • 70
    For those wondering, `array.T` isn't costly, as it just changes the 'strides' of `array` (see [this answer](http://stackoverflow.com/a/19479436/420867) for an interesting discussion) – drevicko Sep 22 '14 at 04:03
  • 3
    Is there a way of iterating which keeps the vectors as column vectors? – Rufus Oct 18 '20 at 14:22
23

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]
Abhijit
  • 59,056
  • 16
  • 119
  • 195
7

You can also use unzip to iterate through the columns

for col in zip(*array):
   some_function(col)
Adi
  • 128
  • 1
  • 8
  • Interesting. This returns tuples instead of arrays. And it's much faster. – Bill Jan 23 '21 at 06:49
  • 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. – Ela782 Apr 02 '21 at 12:32
6

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.

Jean-François Corbett
  • 36,032
  • 27
  • 135
  • 183
stevej
  • 531
  • 5
  • 4
5
for c in np.hsplit(array, array.shape[1]):
    some_fun(c)
agold
  • 5,872
  • 9
  • 40
  • 53
EricX
  • 418
  • 5
  • 6
2

For example you want to find a mean of each column in matrix. Let's create the following matrix

mat2 = np.array([1,5,6,7,3,0,3,5,9,10,8,0], dtype=np.float64).reshape(3, 4)

The function for mean is

def my_mean(x):
    return sum(x)/len(x)

To do what is needed and store result in colon vector 'results'

results = np.zeros(4)
for i in range(0, 4):
    mat2[:, i] = my_mean(mat2[:, i])

results = mat2[1,:]      

The results are: array([4.33333333, 5. , 5.66666667, 4. ])

1

The question is old but for anyone looking nowadays.

You can iterate through the rows of a numpy array like this:

for row in array:
    some_function(row) # do something here

So to iterate through the columns of a 2D array you can simply transpose it like this:

transposed_array = array.T

#Now you can iterate through the columns like this:
for column in transposed_array:
    some_function(column) # do something here

If you want to collect the results of each column into a list for example, you can use list comprehension.

[some_function(column) for column in array.T]

So in summary you can perform a function on each column of an array and collect the results into a list using this line of code:

result_list = [some_function(column) for column in array.T]
Hennio
  • 424
  • 2
  • 18
D.Manasreh
  • 629
  • 4
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29915531) – rv.kvetch Sep 25 '21 at 04:43
  • 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](/help/how-to-answer). – Community Sep 25 '21 at 05:49
0

Alternatively, you can use enumerate. It gives you the column number and the column values as well.

for num, column in enumerate(array.T):
    some_function(column) # column: Gives you the column value as asked in the question
    some_function(num) # num: Gives you the column number 


Banks
  • 161
  • 10
0

list -> array -> matrix -> matrix.T

import numpy as np

list = [1, 99, 2, 2, 14, 5, 3, 12, 7, 4, 43, 1]
arr_n = np.array(list) # list -> array
print(arr_n)
matrix = arr_n.reshape(4, 3) # array -> matrix(4*3)
print(matrix)
print(matrix.T) # matrix -> matrix.T



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

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

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