The Python shape method returns a tuple
denoting the dimensions of a Python object on which it is applied. These Python objects on which the shape
method is applied is usually a numpy.array
or a pandas.DataFrame
. The number of elements in the tuple returned by the shape
method is equal to the number of dimensions in the Python object. Each tuple
element represents the number of elements corresponding to that dimension of the Python object.
Pandas: shape method
The shape
method in Pandas returns a tuple
representing the dimensions i.e. (rows & columns) of the DataFrame
.
1. Check the dimensions of a DataFrame
# 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.
2. Check the dimensions of an empty DataFrame
# 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.
NumPy: shape method
The shape
method in NumPy returns a tuple
representing the dimensions of the numpy array
.
1. Check the dimensions of a numpy 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.
2. Check the dimensions of a numpy array with zero dimensions
# 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.
3. Check the dimensions of a numpy array with one dimension but zero elements
# 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.
Summing-up
In this tutorial, we have learned how to use the shape
method in Python to find out the dimensions of the Python object (NumPy array or Pandas DataFrame).