Python | Pandas df.size, df.shape and df.ndim
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
.ndim are used to return size, shape and dimensions of data frames and series.
Return : Returns size of dataframe/series which is equivalent to total number of elements. That is rows x columns.
Return : Returns tuple of shape (Rows, columns) of dataframe/series
Return : Returns dimension of dataframe/series. 1 for one dimension (series), 2 for two dimension (dataframe)
To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
In this example, the output from size and shape is stored first. Since
.size returns total number of elements, it is compared by multiplying rows and columns returned by the shape method. After that dimension of Dataframe and series is also checked using
Size = 4122 Shape=(458, 9) Shape x Shape = 4122 ndim of dataframe = 2 ndim of series=1
As it can be seen, rows x columns from .shape is equal to the value returned by .size
Also, ndim for dataframe was 2 and series is 1 which is true for all kind of dataframes and series.