This tutorial will introduce the method to get the number of rows of a NumPy array in Python.
NumPy Get the Number of Rows With the
array.shape Property in Python
array.shape property of NumPy arrays is used to get the shape of the array in Python. The shape of an array represents the number of elements in each dimension of an array. The following code example shows us how we can get the number of rows as well as the number of columns of a 2-dimensional NumPy array with the
array.shape property in Python.
import numpy as np array = np.array([[1,3,5],[2,4,6]]) rows, columns = array.shape print("Rows = ",rows) print("Columns = ", columns)
Rows = 2 Columns = 3
We determined the number of rows and columns of the 2-dimensional NumPy array array with the
array.shape property in the above code. We first created a 2-dimensional NumPy array with the
np.array() function. We then determined the number of rows and columns of the array with the
array.shape property and stored the results in
columns variables. In the end, we printed the values inside