NumPy Get Number of Rows in Python

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

The 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)

Output:

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 rows and columns variables. In the end, we printed the values inside rows and columns.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Arrays

  • Get Column of Numpy Array
  • NumPy Value Counts in Python