Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
data science
python
dataframe.size
communitycreator

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

Onyejiaku Theophilus Chidalu

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)
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.

RELATED TAGS

pandas
data science
python
dataframe.size
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring