Numpy how to iterate over columns of array?
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:
You can also use unzip to iterate through the columns
for col in zip(*array):
some_function(col)
Solution 4:
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.