Python

Pandas Print DataFrame Size

In this short article, we will illustrate how to get the size of a Pandas DataFrame using the size property.

Sample DataFrame

The code below provides a sample DataFrame for illustration purposes throughout this article.

# import pandas
import pandas as pd
df = pd.DataFrame({
    'salary': [120000, 100000, 90000, 110000, 120000, 100000, 56000],
    'department': ['game developer', 'database developer', 'front-end developer', 'full-stack developer', 'database developer', 'security researcher', 'cloud-engineer'],
    'rating': [4.3, 4.4, 4.3, 3.3, 4.3, 5.0, 4.4]},
    index=['Alice', 'Michael', 'Joshua', 'Patricia', 'Peter', 'Jeff', 'Ruth'])
print(df)

The code above should return a DataFrame as shown:

Pandas DataFrame Size Attribute

The size attribute returns the size of a DataFrame or Series. This corresponds to the total number of elements in the DataFrame.

The total number of elements in a DataFrame = rows x columns.

The code below shows how to get the size of the DataFrame provided above.

print(df.size)
21

The above returns the total number of elements in the DataFrame.

To get the number of rows and columns in a tuple format, you can use the shape attribute as shown:

print(df.shape)

This should return:

(7, 3)

Conclusion

This article shows how to get the size of a Pandas DataFrame using the size and shape attributes. Check the docs for more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list