How to Get Number of Elements in Pandas DataFrame?


Pandas DataFrame - Get Number of Elements

To get number of elements in a Pandas DataFrame, call DataFrame.size property. The DataFrame.size returns an integer representing the number of elements in this DataFrame. In other words, size property returns (number of rows * number of columns).

Syntax

The syntax to call size property of a DataFrame is

DataFrame.size

Examples

1. Get number of elements in given DataFrame

In this example, we have created a DataFrame and we shall get the number of elements in this DataFrame using DataFrame.size property.

Python Program

import pandas as pd

df = pd.DataFrame(
    [['abc', 22, 22.6],
    ['xyz', 25, 23.2],
    ['pqr', 31, 30.5]],
    columns=['name', 'age', 'bmi'])

number_of_elements = df.size
print(f'Number of elements in this DataFrame = {number_of_elements}')

Output

Number of elements in this DataFrame = 9

Since there are three rows and three columns in this DataFrame, size property returns 3*3 = 9.

Summary

In this tutorial of Python Examples, we learned how to get the number of elements in a DataFrame using DataFrame.size property.