How to obtain the number of elements in a DataFrame in pandas

Overview

To obtain the number of elements in a given pandas DataFrame object, we use the attribute DataFrame.size.

Syntax

DataFrame.size
Syntax for the DataFrame.size attribute

Parameter value

Since it is an attribute, DataFrame.size takes no parameter values.

Return value

The DataFrame.size attribute returns an int representing the number of elements in a DataFrame.

Example

import pandas as pd
# creating a dataframe
df = pd.DataFrame({'AGE': [ 20, 29],
'HEIGHT': [94, 170],
'WEIGHT': [80, 115]})
print(df)
# obtaining the number of elements in df
print("No of elements =", df.size)

Explanation

  • Line 1: We import the pandas library.
  • Lines 4–6: We create a DataFrame, df.
  • Line 9: We print df.
  • Line 11: Using the Data.frame.size attribute, we obtain the number of elements in df.

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