What is the DataFrarme.ndim() function in pandas?
Overview
The ndim attribute returns the number of axes or array dimensions of the dataframe object.
Syntax
DataFrame.ndim
Syntax for the ndim attribute
Parameters
The ndim attribute takes no parameter value.
Return value
The ndim attribute returns an integer value of 1 or 2 representing a Series or DataFrame, respectively.
Example
# A code to illustrate the ndim attribute of a Dataframe# importing the pandas libraryimport pandas as pd# creating a series and a Dataframe objectsa = pd.Series({"row1": 1, "row2": 2, "row3": 3})b = pd.DataFrame({"col1":[1,2], "col2":[3,4]})# obtaining the array dimensionsprint(a.ndim)print(b.ndim)
Explanation
- Line 4: We import the
pandaslibrary. - Line 7–8: We create a series and a DataFrame object,
aandb, respectively. - Line 11: We obtain the array dimensions of
aandb.