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

We can get the size of Pandas DataFrame using the size attribute. The size of the DataFrame is nothing but the number of rows * the number of columns. When it comes to Pandas Series, it will return a number of rows. Using the shape attribute we can get the shape of DataFrame, which is nothing but the number of rows and columns as a tuple.

Advertisements

In this article, I will explain Pandas size attribute and using this how we can get the size of DataFrame and Series with examples.

1. 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)

2. Syntax of DataFrame Size Attribute

Following is the syntax of Pandas size attribute.


# Following is the syntax of size attribute
DataFrame.size

2.1 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.

3. 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.

Let’s read csv file into Pandas DataFrame named student details, where the columns are id, name, class, gender, and marks.


# 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
Pandas DataFrame Size
Pandas DataFrame

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.

size of DataFrame

4. Get the Size of Specific Column of DataFrame

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

5. 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

6. Use info() Method & Get Information of DataFrame

Pandas info() function is used to get the information of a given DataFrame. This function returns number of columns, column labels, column data types, memory usage, range index, and the number of cells in each column (non-null values).


# 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

7. 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.

8. 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.

9. Get the Number of Columns

Using len(df.columns) is another way to get the number of columns in a Pandas 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

10. 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, I have explained Pandas size attribute and using this how we can get the size of Pandas DataFrame, Series, and empty DataFrame. And also explained using various attributes how we can get the shape, dimensions, and total information of a given DataFrame with examples.

Happy learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.