Iterate Over Rows of a Numpy Array in Python

Use a Nested
for
Loop to Iterate Over Rows of a Numpy Array in Python 
Use a
for
Loop and theflatten()
Function to Iterate Over Rows of a Numpy Array in Python 
Use the
apply_along_axis()
Function to Iterate Over Rows of a Numpy Array in Python
Python mainly utilizes the NumPy
library to allow the implementation of arrays in its code; these arrays can be ndimensional. Iterating over the elements of an array is one of the few things a programmer may encounter while implementing arrays.
This tutorial demonstrates how to iterate over rows of a NumPy
array in Python.
The concept of rows and columns does not exist in a onedimensional array; therefore, we will discuss arrays with at least two dimensions. Most of the methods and implementations in this article would be carried out on a twodimensional array.
Use a Nested for
Loop to Iterate Over Rows of a Numpy Array in Python
For iterating the elements of a single row, we may be able to utilize the for
loop alone. However, for iterating over multiple rows of a NumPy
array, we need to nest the for
loop.
The working is straightforward, and the code uses nested for
loops to access and iterate over all the elements of an array rowwise.
The following code uses a nested for
loop to iterate over rows of a NumPy
array in Python.
import numpy as np
x = np.matrix([[21,22,23],
[24,25,26],
[27,28,29]])
for row in x:
print (str(row))
The above code provides the following output:
[[21 22 23]]
[[24 25 26]]
[[27 28 29]]
Here, we used the print
command to print every row. If more details are given, then any function can also be implemented over the rows of a NumPy
array.
Use a for
Loop and the flatten()
Function to Iterate Over Rows of a Numpy Array in Python
Instead of nesting the for
loop, we can take an alternative route, which uses the flatten()
function to iterate over rows of a NumPy
array in Python. The flatten()
function can convert a twodimensional array into a onedimensional one, which makes it possible to get the result needed by applying the for
loop just once in the program.
The following code uses a for
loop and the flatten()
function to iterate over rows of a NumPy
array in Python.
import numpy as np
x = np.matrix([[21,22,23],
[24,25,26],
[27,28,29]])
for cell in x.flatten():
print(cell, end=' ')
The above code provides the following output:
[[21 22 23 24 25 26 27 28 29]]
Use the apply_along_axis()
Function to Iterate Over Rows of a Numpy Array in Python
The NumPy
library provides the NumPy.apply_along_axis()
function that can apply a function to the elements of an array along any axis specified by the programmer. The apply_along_axis()
function has a simple syntax, which takes in the function that the programmer needs to implement, the axis specified, and the array on which the implementation needs to occur.
The following code uses the apply_along_axis()
function to iterate over rows of a NumPy
array in Python.
import numpy as np
x = np.matrix([[21,22,23],
[24,25,26],
[27,28,29]])
def myfunction(a):
return a
print(np.apply_along_axis(myfunction, axis=1, arr=x))
The above code provides the following output:
[[21 22 23]
[24 25 26]
[27 28 29]]
Vaibhhav is an IT professional who has a stronghold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.
LinkedInRelated Article  NumPy Array
 Python NumPy Shift Array
 Write NumPy Array to CSV in Python
 Convert 3D Array to 2D Array in Python
 Shape and Size of Array in Python