Trusted answers to developer questions

What is the DataFrarme.ndim() function in pandas?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

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.

RELATED TAGS

pandas

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
Did you find this helpful?