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 dataframedf = pd.DataFrame({'AGE': [ 20, 29],'HEIGHT': [94, 170],'WEIGHT': [80, 115]})print(df)# obtaining the number of elements in dfprint("No of elements =", df.size)
Explanation
- Line 1: We import the
pandaslibrary. - Lines 4–6: We create a DataFrame,
df. - Line 9: We print
df. - Line 11: Using the
Data.frame.sizeattribute, we obtain the number of elements indf.