Skip to Content

Pandas – Get DataFrame Size (With Examples)

Pandas is a powerful data manipulation library in Python and comes with a number of useful built-in features and properties to work with tabular (2D) data. In this tutorial, we will look at how to get the size of a dataframe in pandas with the help of some examples.

What is size of a dataframe in Pandas?

size of a dataframe in pandas

The size of a pandas dataframe refers to the number of items (or cells) in the dataframe. Since a dataframe is a two-dimensional data structure, its size is the number of rows * number of columns. For example, the size a dataframe with 4 rows and 3 columns is 12.

How to get the size of a dataframe in Pandas?

You can use the pandas dataframe size property to get the size of a dataframe. The following is the syntax –

# get dataframe size
df.size

It returns an integer representing the objects (items) in the dataframe. That is, the number of rows * the number of columns.

You can access a pandas Serie’s size property which return the count of values (length) in the series.

Examples

Let’s now look at some examples of using the above syntax on a dataframe in Pandas.

First, we will create a sample dataframe that we will be using throughout this tutorial.

import numpy as np
import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

pandas dataframe of employees with 4 rows and 2 columns

Here, we created a dataframe having the “Name” and “Age” data of some employees in an office. You can see that the above dataframe has 4 rows and 2 columns.

Example 1 – Size of a pandas dataframe using size property

Let’s get the size of the dataframe created above using its size property.

# get dataframe size
print(df.size)

Output:

8

We get the size of the above dataframe as 8 (which is 4*2, rows*colums).

Note that the size property returns the size of a dataframe irrespective of values (whether they are NaN or non-NaN).

Let’s recreate the employee dataframe with the same number of rows and columns but this time having some values as NaN and then compute its size.

# employee data with some NaN values
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, np.nan, np.nan, 32]
}

# create pandas dataframe with some Nan values
df = pd.DataFrame(data)

# get dataframe size
print(df.size)

Output:

8

We get the size of the dataframe as 8 (same as what we got above).

Example 2 – Size of a pandas dataframe using its shape property

Alternatively, you can also calculate the size of a dataframe in pandas by accessing its .shape property which return a tuple of (number of rows, number of columns). Multiply the two values together to the size.

# get dataframe shape
print("Shape: ", df.shape)
# get dataframe size
print("Size: ", df.shape[0]*df.shape[1])

Output:

Shape:  (4, 2)
Size:  8

You can see that we get the shape of the dataframe df as (4, 2) and multiplying them together gives us the size of the dataframe.

Length of a dataframe in Pandas

Note that the size of a dataframe is different from its length. The size of a dataframe in pandas is the total number of objects (or cells) in the dataframe whereas the length of a dataframe is the total number of rows in the dataframe.

You can use the built-in len() function in Python or the first value in the tuple returned by the shape property to get a dataframe’s length.

# get dataframe length using len() function
print(len(df))
# get dataframe length using .shape property
print(df.shape[0])

Output:

4
4

We get the length of the dataframe df as 4 (whereas we got its size as 8).

Pop Quiz

The following are some quick questions to test your understanding of the topics mentioned in this tutorial –

The size of a dataframe in pandas refers to the number of items (or cells) in the dataframe. It is equal to the number of rows * the number of columns.

Use the pandas dataframe size property to get the size of a dataframe in Pandas.

The length of a dataframe refers to the number of rows in the dataframe whereas its size represents the number of elements (or cells) in the dataframe which is the number of rows * the number of columns.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.