Get Number of Rows in NumPy

Get Number of Rows in NumPy

This tutorial will introduce how to get the number of rows of a NumPy array.

Get the Number of Rows in NumPy Array With the array.shape Property

The array.shape property of NumPy arrays gets the shape of the array. The shape of an array represents the number of elements in each dimension of an array. The following code example shows how to get the number of rows as well as the number of columns of a 2D NumPy array with the array.shape property.

import numpy as np

array = np.array([[1,3,5],[2,4,6]])

rows, columns = array.shape

print("Rows = ",rows)
print("Columns = ", columns)

Output:

Rows =  2
Columns =  3

We first created a 2D 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 rows and columns variables. In the end, we printed the values inside rows and columns.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn