• 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
  • VBA
  • 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.  Remove Brackets from String Using Python
  • 2.  pandas median – Find Median of Series or Columns in DataFrame
  • 3.  How to Save and Use Styles from Existing Word File With python-docx
  • 4.  Replace Values in Dictionary in Python
  • 5.  Python Get Operating System Information with os and platform Modules
  • 6.  Python Check if List Index Exists Using Python len() Function
  • 7.  Intersection of Two Lists in Python
  • 8.  Check if String Does Not Contain Substring in Python
  • 9.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 10.  Using Python to Find Second Smallest Value in 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy