×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

How to fix 'Why does the shape of a 1D array not show the number of rows as 1'?

Learn about the shape() function and why does shape() function does not given the number of rows as 1 for a 1D array?
By Pranit Sharma Last updated : December 26, 2023

Python - numpy.shape() Method

The numpy.shape(a) is used to return the shape of a numpy array (a). By this, we mean that this function gives the dimensions of a numpy array i.e., the number of rows and columns.

It returns a tuple with two values, the first is the number of rows and the other is the number of columns.

If we apply numpy.shape() on a 1D numpy array, it will not return the number of rows as 1. This is because the concept of rows and columns applies when you have a 2D array. However, if we want the shape function to return 1 as the number of rows, we can convert it into a 2D array by giving only one row.

Let us understand with the help of an example,

Python program to demonstrate the example of numpy.shape()

# Import numpy
import numpy as np

# Creating a numpy array
arr = np.array([1,2,3,4,5])

# Display original array
print("Original array:\n",arr,"\n")

# Display shape
print("Shape:\n",arr.shape,"\n")

# Converting into 2d array and returning shape
arr = np.array([[1,2,3,4,5]])
print("Shape of array:\n",arr.shape,"\n")

Output

Example: How to fix 'Why does the shape of a 1D array not show the number of rows as 1'?

In this example, we have used the following Python basic topics that you should learn:

Python NumPy Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.