Python Shape Function: Find Dimensions of Arrays and DataFrames

Python Shape Method

The shape function in Python is used to determine the dimensions of arrays and dataframes. This information is provided in the form of a tuple. Typically, the shape method is applied to Python objects such as numpy.array or pandas.DataFrame.

The number of elements in the tuple returned by the shape function corresponds to the dimensions of the Python object. Each element within the tuple signifies the number of elements pertaining to the given Python object.

Python Shape Function

The shape function in Python yields a tuple that signifies the dimensions of a NumPy array or a Pandas DataFrame. In the case of a DataFrame, the tuple indicates the quantity of rows and columns. For a NumPy array, the tuple reveals the count of elements across every dimension. This function is beneficial for comprehending the structure and magnitude of data objects within Python.

The shape function in Pandas provides the dimensions (rows & columns) of the DataFrame as a tuple.

Example 1: Check the dimensions of a DataFrame

In this illustration, we’ll be generating a Pandas DataFrame utilizing a Python list and employing the shape function to examine its dimensions.

This enables us to grasp the quantity of rows and columns present in the DataFrame, which proves beneficial when handling extensive datasets or during the preprocessing phase for data analysis.

# Import Pandas Python module
import pandas as pd 

# Create a Python list
ls =[['A','B','C','D'], ['e' ,'f' ,'g' ,'h'], [11, 22, 33, 44]]

# Create a Pandas DataFrame from the above list
df = pd.DataFrame(ls)

# Print the DataFrame
print(df)

# Check the dimensions of the DataFrame
print(df.shape)

Output:

    0   1   2   3 
0   A   B   C   D 
1   e   f   g   h 
2  11  22  33  44 
(3, 4)

The shape method has returned a tuple (3, 4) with two elements depicting the DataFrame has two dimensions with three rows and four columns.

Example 2: Check the Dimensions of an Empty DataFrame

In this example, we’ll explore how to utilize the shape attribute on an empty DataFrame to verify its emptiness by examining its dimensions. This technique can be particularly useful for ensuring that a DataFrame is indeed empty before performing further operations or analysis.

# Import Pandas Python module
import pandas as pd 

# Create an empty Pandas DataFrame
df = pd.DataFrame()

# Print the DataFrame
print(df)

# Check the dimensions of the empty DataFrame
print(df.shape)

Output:

Empty DataFrame 
Columns: [] 
Index: [] 
(0, 0)

The shape method has returned a tuple (0, 0) with two elements depicting the DataFrame has two dimensions with zero rows and zero columns.

Using Shape in NumPy

The shape function in NumPy provides the dimensions of the numpy array as a tuple.

Example 3: Check the Dimensions of an Array

In this example, we will create a three-dimensional NumPy array and use the shape function to find its dimensions. The output tuple will have three elements, each representing the number of elements in the respective dimension of the array.

# Import Python NumPy module
import numpy as np

# Define a numpy array with zero dimensions
arr = np.array([[[1,2] ,[3,5]], [[2,3] ,[4,7]], [[3,4] ,[5,8]]])

# Print the numpy array
print(arr)

# Check the dimensions of arr
print(arr.shape)

Output:

[[[1 2 3] 
  [3 5 6]]] 
(1, 2, 3)

The shape method has returned a tuple (1, 2, 3) with three elements depicting the array has three dimensions where each dimension has one, two, and three elements respectively.

Example 4: Check the Dimensions of a NumPy Array with Zero Dimensions

In certain situations, you might encounter an array with zero dimensions. This can happen when you create an array with a single scalar value. In this example, we will demonstrate how to check the dimensions of such a NumPy array.

# Import Python NumPy module
import numpy as np

# Define a numpy array with zero dimensions
arr = np.array(0)

# Print the numpy array
print(arr)

# Check the dimensions of arr
print(arr.shape)

Output:

0 
()

The shape method has returned an empty tuple () with zero elements depicting the array has zero dimensions.

Example 5: Check the Dimensions of a NumPy Array with One Dimension and Zero Elements

In this example, we will create an array with only one dimension but containing zero elements. This can occur when you initialize an array from an empty list or when you need a placeholder for data that will be added later. We will then use the shape function to examine its dimensions.

# Import Python NumPy module
import numpy as np

# Define a numpy array from an empty list
arr = np.array([])

# Print the numpy array
print(arr)

# Check the dimensions of arr
print(arr.shape)

Output:

[] 
(0,)

The shape method has returned a tuple (0,) with one element depicting the array has only one dimension with zero elements.

FAQs

What does shape function do in python?

The shape function in Python is used to find the dimensions of data structures, such as NumPy arrays and Pandas DataFrames. It returns a tuple representing the dimensions, with each tuple element corresponding to the number of elements in that dimension. This function is useful for understanding the structure and size of data objects in Python.

How to use shape function in python?

To use the shape function in Python, first import the Pandas library with import pandas as pd and create a DataFrame using df = pd.DataFrame(data). Then, obtain the dimensions as a tuple using dimensions = df.shape. For NumPy arrays, import the NumPy library using import numpy as np, create an array with arr = np.array(data), and get the dimensions with dimensions = arr.shape. The shape function returns a tuple representing the dimensions of the Python data structure.

Summing-up

In this tutorial, we’ve learned how to use the shape function in Python to find the dimensions of NumPy arrays and Pandas DataFrames. This versatile function is essential for understanding the structure and size of your data objects. How might you apply this in your next data analysis project?

References: