Open In App

Python – Iterate over Columns in NumPy

Improve
Improve
Like Article
Like
Save
Share
Report

Numpy (abbreviation for ‘Numerical Python‘) is a library for performing large-scale mathematical operations in a fast and efficient manner. This article serves to educate you about methods one could use to iterate over columns in an 2D NumPy array. Since a single-dimensional array only consists of linear elements, there doesn’t exists a distinguished definition of rows and columns in them. Therefore, in order to perform such operations we need an array whose len(ary.shape) > 1 . To install NumPy on your python environment, type the following code in your OS’s Command Processor (CMD, Bash etc):

pip install numpy

We would be taking a look at several methods of iterating over a column of an Array/Matrix:- 

METHOD 1: CODE: Use of primitive 2D Slicing operation on an array to get the desired column/columns 

Python3




import numpy as np
 
# Creating a sample numpy array (in 1D)
ary = np.arange(1, 25, 1)
 
# Converting the 1 Dimensional array to a 2D array
# (to allow explicitly column and row operations)
ary = ary.reshape(5, 5)
 
# Displaying the Matrix (use print(ary) in IDE)
print(ary)
 
# This for loop will iterate over all columns of the array one at a time
for col in range(ary.shape[1]):
    print(ary[:, col])


Output:

[[ 0,  1,  2,  3,  4],
 [ 5,  6,  7,  8,  9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]])


[ 0  5 10 15 20]
[ 1  6 11 16 21]
[ 2  7 12 17 22]
[ 3  8 13 18 23]
[ 4  9 14 19 24]

Time complexity: O(n^2) where n is the size of the array.
Auxiliary space: O(n) as only one new array is created during the reshaping operation.

Explanation: In the above code, we firstly create an linear array of 25 elements (0-24) using np.arange(25). Then we reshape (transform 1D to 2D) using np.reshape() to create a 2D array out of a linear array. Then we output the transformed array. Now we used a for loop which would iterate x times (where x is the number of columns in the array) for which we used range() with the argument ary.shape[1] (where shape[1] = number of columns in a 2D symmetric array). In each iteration we output a column out of the array using ary[:, col] which means that give  all elements of the column number = col

METHOD 2: In this method we would transpose the array to treat each column element as a row element (which in turn is equivalent of column iteration). 

Code: 

Python3




# libraries
import numpy as np
 
# Creating an 2D array of 25 elements
ary = np.array([[ 01234],
                [ 56789],
                [10, 11, 12, 13, 14],
                [15, 16, 17, 18, 19],
                [20, 21, 22, 23, 24]])
 
 
# This loop will iterate through each row of the transposed
# array (equivalent of iterating through each column)
for col in ary.T:
    print(col)


Output:

[ 0  5 10 15 20]
[ 1  6 11 16 21]
[ 2  7 12 17 22]
[ 3  8 13 18 23]
[ 4  9 14 19 24]

Time complexity: O(n*m)
Auxiliary space: O(m)

Explanation: Firstly, we created an 2D array (same as the previous example) using np.array() and initialized it with 25 values. Then we transposed the array, using ary.T which in turn switches the rows with the columns and columns with the rows. Then we iterated over each row of this transposed array and printed the row values.



Last Updated : 26 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads