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.

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

Following are the quick example of getting the size of DataFarme.


# Below are the quick examples
 
# 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 Pandas 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.


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

# Output:
# 35

5. Get the Size of Empty DataFrame

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

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)

8. Get the Number of rows in Dataframe

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

9. Get the Number 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

Use Pandas ndim attribute to get the number of dimensions of a DataFrame. Let’s use this attribute and get the number of dimensions of DataFrame.


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

# Output:
# 2

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

Leave a Reply