• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas DataFrame size – Get Number of Elements in DataFrame or Series

pandas DataFrame size – Get Number of Elements in DataFrame or Series

January 21, 2022 Leave a Comment

To get the total number of elements in a pandas DataFrame, we can use the pandas DataFrame size property.

df.size  # Returns number of rows times number of columns

You can also find the number of elements in a DataFrame column or Series using the pandas size property.

df["Column"].size # Returns number of rows

When working with data, it is useful for us to be able to find the number of elements in our data. When working with pandas DataFrames, we can find the total number of elements in a DataFrame with the pandas DataFrame size() property.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Age': [43,23,71,49,52,37], 
      'Test_Score':[90,87,92,96,84,79]})

print(df)
# Output: 
   Age  Test_Score
0   43          90
1   23          87
2   71          92
3   49          96
4   52          84
5   37          79

To get the size of this DataFrame, we access the size property in the following Python code.

print(df.size)

# Output:
12

Getting Size of Column in pandas DataFrame

To get the size of a column in pandas, we can access the size property in the same way as above. The size of a column is the total number of rows in that column.

Let’s say we have the same DataFrame as above and want to find the number of rows in the column “Age”. We can do so easily with the following Python code:

print(df["Age"].size)

# Output:
6

Getting the Size of a Series in pandas

To get the size of a Series in pandas, we can access the size property in the same way as above. The size of a Series is the total number of rows in that column.

Let’s say we have the same DataFrame as above and want to find the number of rows in the Series created from the column “Age”. To make a series, we convert the column to a Series, and then access the size property.

print(pd.Series(df["Age"]).size)

# Output:
6

As expected, this is the same result as accessing the size property on the column “Age”.

Hopefully this article has been helpful for you to understand how to find the size of a pandas DataFrame and get the total number of elements in a DataFrame.

Other Articles You'll Also Like:

  • 1.  Union of Lists in Python
  • 2.  Using Python to Print Plus or Minus Sign Symbol
  • 3.  Python Print List – Printing Elements of List to the Console
  • 4.  Python Check if Float is a Whole Number
  • 5.  Length of Dictionary Python – Get Dictionary Length with len() Function
  • 6.  Check if Variable is Datetime in Python
  • 7.  Python Delete Variable – How to Delete Variables with del Keyword
  • 8.  Comparing Datetimes in Python
  • 9.  Format Numbers as Dollars in Python with format()
  • 10.  Using Python to Remove Duplicates from List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy