Pandas DataFrame.size


Pandas DataFrame.size

The DataFrame.size property in pandas is used to return the total number of elements in the DataFrame. It is equal to the number of rows multiplied by the number of columns.


Syntax

The syntax for accessing the size property is:

DataFrame.size

Here, DataFrame refers to the pandas DataFrame whose total number of elements is being calculated.


Returns

An integer representing the total number of elements in the DataFrame.


Examples

Basic Usage

Use the size property to get the total number of elements in a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 28],
    'Salary': [70000.5, 80000.0, 75000.0]
}
df = pd.DataFrame(data)

# Get the total number of elements
print("Total Number of Elements in the DataFrame:")
print(df.size)

Output

Total Number of Elements in the DataFrame:
9

Calculating Elements with Empty DataFrames

When the DataFrame is empty, the size property will return 0.

Python Program

import pandas as pd

# Create an empty DataFrame
df_empty = pd.DataFrame()

# Get the total number of elements in the empty DataFrame
print("Total Number of Elements in the Empty DataFrame:")
print(df_empty.size)

Output

Total Number of Elements in the Empty DataFrame:
0

Using the Size Property with Larger DataFrames

The size property is particularly useful when working with large DataFrames to quickly determine the total number of elements.

Python Program

import pandas as pd

# Create a larger DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya', 'Ravi', 'Maya'],
    'Age': [25, 30, 28, 22, 35],
    'Salary': [70000.5, 80000.0, 75000.0, 60000.0, 90000.0]
}
df_large = pd.DataFrame(data)

# Get the total number of elements
print("Total Number of Elements in the Larger DataFrame:")
print(df_large.size)

Output

Total Number of Elements in the Larger DataFrame:
15

Summary

In this tutorial, we explored the DataFrame.size property in pandas. Key points include:

  • Using size to get the total number of elements in a DataFrame
  • Understanding how size behaves with empty DataFrames
  • Using size with larger DataFrames for quick calculations

The DataFrame.size property provides a simple and efficient way to determine the total number of elements in a DataFrame, which can be useful in many data analysis tasks.


Python Libraries