• Post author:
  • Post category:Pandas
  • Post last modified:May 9, 2024
  • Reading time:19 mins read
You are currently viewing How to Get Size of Pandas DataFrame?

You can get the size of a Pandas DataFrame using the DataFrame.size attribute. This attribute returns the number of elements in the DataFrame, which is equal to the number of rows multiplied by the number of columns. The size of the DataFrame is nothing but the number of rows * the number of columns.

Advertisements

In this article, I will explain how to get Size of Pandas DataFrame, its syntax, parameters, and usage of how to get the size of a Pandas DataFrame using the DataFrame.size attribute.

Key Points –

  • Use the size attribute to obtain the total number of elements in a Pandas DataFrame.
  • The size attribute returns the product of the number of rows and columns in the DataFrame.
  • The size attribute is useful for understanding the memory footprint of the DataFrame.
  • Even for an empty DataFrame, the size attribute returns 0, indicating the absence of elements.

Quick example of Get Size of DataFrame

If you are in a hurry, below are some quick examples of getting the size of DataFarme.


# Quick example of get size of dataframe
 
# Example 1:  Get the size of Pandas dataframe
print(" Size of DataFrame:", df.size)

# Example 2:Get shape of Pandas Series
print(df['class'].size)

# Example 3: Get empty DataFrame size
print("Get the size of empty DataFrame:", df.size)

# Example 4: Get the shape of Pandas dataframe
print(" Shape of DataFrame:", df.shape)

# Example 5: Get the information of the dataframe
print(df.info())

# Example 6: Get the length of rows
print(len(df))

# Example 7: Get the number of columns in a dataframe
print(len(df.columns))

# Example 8: Get the dimensions of dataframe
print(df.ndim)

Syntax of DataFrame Size()

Following is the syntax of the Pandas size attribute.


# Following is the syntax of size attribute
DataFrame.size

Return Value

It returns the product of a number of columns and the number of rows of a given DataFrame. If Series, it returns a number of rows.

Pandas Get Size of the DataFrame 

The size attribute is used to get the size of the Pandas DataFrame which is the total number of elements in the DataFrame or Series. When we apply this attribute to DataFrames, it will return the product of the number of rows and columns. Whereas, for Series, it will return the number of rows.

To run some examples of getting the size of the pandas DataFrme, let’s read csv file into Pandas DataFrame named student details.


# Read csv file into DataFrame
import pandas as pd
df= pd.read_csv("/apps/student_details.csv")
print(df)
print(df.head())
print(df.tail())

Yields below output.

Pandas DataFrame size
Pandas DataFrame Size

Let’s apply the size attribute on the above DataFrame and get the size.


# Get the size of Pandas dataframe
print(" Size of DataFrame:", df.size)

Yields below output.

Retrieve the Size of the Column

Since the DataFrame column is nothing but a Series, here, I will select a single column and get the size of the column. For instance, The df['class'].size expression retrieves the ‘class’ column from the DataFrame df and then calculates the size (number of elements) of that column using the .size attribute.


# Get shape of Pandas Series
print(df['class'].size)

# Output:
# 35

Get the Size of Empty DataFrame

The size of an empty Pandas DataFrame is zero, as there are no elements in the DataFrame. The size attribute of a DataFrame provides the total number of elements (rows * columns), and for an empty DataFrame, both the number of rows and columns are zero.

We can get the size of an empty DataFrame using the size attribute. Let’s create an empty DataFrame and then, apply the size attribute, it will return the size of the given DataFrame as 0.


# create empty DateFrame
df = pd.DataFrame()
print(df)
# Get empty DataFrame size
print("Get the size of empty DataFrame:", df.size)

# Output:
# Empty DataFrame
# Columns: []
# Index: []
# Get the size of empty DataFrame: 0

Use info() Method & Get Information of DataFrame

You can use the info() method in Pandas to get a concise summary of a DataFrame, including information about its columns, data types, memory usage, and more.


# Get the information of the dataframe
print(df.info())

# Output:
# RangeIndex: 35 entries, 0 to 34
# Data columns (total 5 columns):
#     Column  Non-Null Count  Dtype 
# ---  ------  --------------  ----- 
#  0   id      35 non-null     int64 
#  1   name    35 non-null     object
#  2   class   35 non-null     object
#  3   mark    35 non-null     int64 
#  4   gender  35 non-null     object
# dtypes: int64(2), object(3)
# memory usage: 1.5+ KB
# None

Get the Shape of Pandas DataFrame

To get the shape of a Pandas DataFrame, you can use the shape attribute. The shape attribute returns a tuple representing the dimensions of the DataFrame, where the first element is the number of rows, and the second element is the number of columns.

The shape attribute is used to get the shape of DataFrame or Pandas Series, it returns a number of rows and columns as a tuple. For Series, it returns a number of rows in the form of a tuple.


# Get the size of dataframe
print(df.shape)

# Output:
# (35, 5)

In the above example, df.shape returns a tuple (35, 5), indicating that the DataFrame has 35 rows and 5 columns.

Get the Number of rows in DataFrame

Using len(df) is another way to get the number of rows in a Pandas DataFrame. The len() function applied to a DataFrame returns the number of rows.

Using len() function we can get the number of rows and columns of the Dataframe. It returns the length of rows when we use len(df) this syntax. Let’s apply and get the number of rows of a given DataFrame.


# Get the length of rows
print(len(df))

# Output:
# 35

Both len(df) and df.shape[0] will give you the same result, which is the number of rows in the DataFrame. You can choose the method that you find more readable or convenient for your specific use case.

Get Number of Columns

You can use len(df.columns) is an alternative approach to determining the number of columns in a Pandas DataFrame. This method retrieves the length of the columns attribute, providing the count of columns present in the DataFrame.

The columns attribute of a DataFrame contains the column labels, and applying len to it will give you the count of columns. Using len(df.columns) syntax we can get the number of columns in a DataFrame. Let’s apply and get the count.


# Get the number of columns in a dataframe
print(len(df.columns))

# Output:
# 5

Get Number of Dimensions

In the context of a Pandas DataFrame, the number of dimensions typically refers to the rank of the data structure. A DataFrame is a two-dimensional labeled data structure, so it has two dimensions: rows and columns.

If you want to explicitly get the number of dimensions, you can use the ndim attribute. The ndim attribute returns the number of dimensions of the DataFrame. Let’s use this attribute and get the number of dimensions of DataFrame.


# Get the dimensions of dataframe
print(df.ndim)

# Output:
# 2

As expected, the ndim attribute returns 2 for a DataFrame, indicating that it has two dimensions.

Frequently Asked Questions on Get Size of Pandas DataFrame

How do I get the size of a Pandas DataFrame?

To get the size of a Pandas DataFrame, you can use the size attribute. The size attribute returns the total number of elements in the DataFrame, which is the product of the number of rows and columns.

How do I get the shape of a Pandas DataFrame?

To get the shape of a Pandas DataFrame, you can use the shape attribute. The shape attribute returns a tuple representing the dimensions of the DataFrame, where the first element is the number of rows and the second element is the number of columns.

Can I use len(df) to get the number of rows in a DataFrame?

You can use len(df) to get the number of rows in a Pandas DataFrame. The len function applied to a DataFrame returns the number of rows.

How can I get the number of columns in a Pandas DataFrame?

You can use either df.shape[1] or len(df.columns) to get the number of columns in a DataFrame.

What does the ndim attribute represent in a Pandas DataFrame?

The ndim attribute in a Pandas DataFrame represents the number of dimensions. For a DataFrame, which is a two-dimensional data structure, ndim always returns 2. This is because a DataFrame has both rows and columns, making it a two-dimensional object.

Is there a difference between the size of a DataFrame and the number of elements in a specific column?

The size of a DataFrame (df.size) gives the total number of elements (rows * columns), while the number of elements in a specific column can be obtained using len(df['column_name']) or df['column_name'].size. The latter represents the size of that particular column.

Conclusion

In this article, you have learned how to get the size of Pandas DataFrame by using the DataFrame.size attribute.

Happy learning!!