Pandas Series.nbytes


Pandas Series.nbytes property

In this tutorial, we'll go through examples for the Pandas Series.nbytes property, which returns the number of bytes in the Series data.


Examples for Series.nbytes property

1. Getting the number of bytes in given Pandas Series

In the following program, we take a Series object in series_data, and get the number of bytes in this Series object using Series.nbytes property.

Python Program

import pandas as pd

# Create a Pandas Series
series_data = pd.Series([10, 20, 30, 40, 50])

# Get number of bytes in the Series data
nbytes = series_data.nbytes

# Display the result
print(f"Number of bytes in the Series object\n{nbytes}")

Output

Number of bytes in the Series object
40

There are five int64 values in the given series object. Since each int64 element takes 8 bytes, the Series object series_data takes 40 bytes in total.


2. Getting the number of bytes in given Pandas Series with String values

In the following program, we take a Series object in series_data with string values, and get the number of bytes in this Series object using Series.nbytes property.

Python Program

import pandas as pd

# Create a Pandas Series
series_data = pd.Series(["apple", "banana", "cherry"])

# Get number of bytes in the Series data
nbytes = series_data.nbytes

# Display the result
print(f"Number of bytes in the Series object\n{nbytes}")

Output

Number of bytes in the Series object
24


Python Libraries