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 library
import pandas as pd
# creating a series and a Dataframe objects
a = pd.Series({"row1": 1, "row2": 2, "row3": 3})
b = pd.DataFrame({"col1":[1,2], "col2":[3,4]})
# obtaining the array dimensions
print(a.ndim)
print(b.ndim)

Explanation

  • Line 4: We import the pandas library.
  • Line 7–8: We create a series and a DataFrame object, a and b, respectively.
  • Line 11: We obtain the array dimensions of a and b.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources