Hello, readers! This article talks about the Python shape() method and its variants in programming with examples.
So, let us begin!!
Use of Python shape() method
When it comes to the analysis of data and its variants, it is extremely important to realize the volume of data. That is, before we plan to analyze the data and perform synthesis on it, we need to be aware of the dimensions of the data.
This is when the Python shape() method comes into the picture.
With the shape() method, comes the flexibility to obtain the dimensions of any Python object. Yes, it returns a tuple value that indicates the dimensions of a Python object.
To understand the output, the tuple returned by the shape() method is the actual number of elements that represent the value of the dimension of the object.
Usually, on a broader scale, the shape() method is used to fetch the dimensions of Pandas and NumPy type objects in python.
Every value represented by the tuple corresponds to the actual dimension in terms of array or row/columns.
Let us now have a look at the variant of the same in the upcoming section.
Variant 1: Pandas shape attribute
When we try to associate the Pandas type object with the shape method looking for the dimensions, it returns a tuple that represents rows and columns as the value of dimensions.
We usually associate shape as an attribute with the Pandas dataframe to get the dimensions of the same.
In this example, we have created a dataframe from a Python list using DataFrame() method. Post which, we apply the dataframe.shape to check for the dimensions.
As the data that we have passed has two rows and two columns (2×2), the shape method returns us the number of rows and columns as the result.
import pandas as pd data =[['P','Q'], [0, 1]] data_frame = pd.DataFrame(data) print(data_frame) print("Shape of the data frame:") print(data_frame.shape)
0 1 0 P Q 1 0 1 Shape of the data frame: (2, 2)
In this example, we have created an empty dataframe using DataFrame() function. Then, with the shape() method, we can get the dimensions of the empty dataframe
import pandas as pd data_frame = pd.DataFrame() print(data_frame) print("Shape of the data frame:") print(data_frame.shape)
Empty DataFrame Columns:  Index:  Shape of the data frame: (0, 0)
Variant 2: NumPy shape method
With NumPy data structure, we store data elements in the form of an array. When we associate the shape() method with the NumPy array, the dimensions of the array are represented in the form of a tuple.
Here, we have created a NumPy array with no dimensions. Further, we have applied the shape() method on the array to get the dimensions of the created array.
import numpy as np ar = np.array(0) print(ar) print("Shape of the array:") print(ar.shape)
0 Shape of the array: ()
In this example, we have created a NumPy array and added elements to it. This is achieved using numpy.array() function. Now, we apply the shape() method to the array of elements.
import numpy as np ar = np.array([[12,20] ,[13,15]]) print(ar) print("Shape of the array:") print(ar.shape)
[[12 20] [13 15]] Shape of the array: (2, 2)
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Kubernetes, Stay tuned with us.
Till then, Happy Learning! 🙂