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

Error: Code Block Widget Crashed, Please Contact Support

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)
Implementing the DataFrame.size attribute

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.

Free Resources