Python | Pandas df.size, df.shape and df.ndim

Python Methods and Functions

Pandas .size , .shape and .ndim are used to return the size, shape and dimensions of data frames and series.

Syntax: dataframe.size
Return: Returns size of dataframe / series which is equivalent to total number of elements. That is rows x columns.

Syntax: dataframe.shape
Return: Returns tuple of shape (Rows, columns) of dataframe / series

Syntax: dataframe.ndim
Return: Returns dimension of dataframe / series. 1 for one dimension (series), 2 for two dimension (dataframe)

To load the dataset used in the following example, click here.

In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below. 

Example:
This example outputs the size and shape are saved first. Since .size returns the total number of elements, it is compared by multiplying the rows and columns returned by the shape method. After that, the Dataframe and Series dimension is also checked with .ndim

# pandas module import

import pandas as pd

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

 
# dataframe.size

size = data.size

  
# dataframe.shape

shape = data.shape

 
# dataframe.ndim

df_ndim = data.ndim

 
# series .ndim

series_ndim = data [ "Salary" ]. ndim

 
# print size and shape

print ( " Size = {} Shape = {} Shape [0] x Shape [1] = {} "

format (size, shape, shape [ 0 ] * shape [ 1 ]))

 
# print ndim

print ( "ndim of dataframe = {} ndim of series = {}" .

format (df_ndim, series_ndim))

Output :

 Size = 4122 Shape = (458, 9) Shape [0] x Shape [1] = 4122 ndim of dataframe = 2 ndim of series = 1 

As you can see, the x column rows from .shape are equal to the value returned by .size
Also, the ndim for the dataframe was 2, and the series = 1, which is true for all kinds of dataframes and series.