The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Number Of Rows – 6 Methods

Count rows in pandas DataFrame using six methods

Often times you’ll need to know how many rows are in your dataset. Simply, it is the most foundational metric you can know about your data. Plus, you may want to find out how long your .apply() function is going run. We’ll show your 6 pandas ways to count number of rows.

Pandas number of rows will tell you…drumroll…how many rows you have in your dataset. This is important to know before applying an expensive (long running) function to your dataset. It is crucial to understand while getting to know your data.

Lets go over 6 methods in order of our favorites

  1. DataFrame Length – len(df)
  2. DataFrame Info – df.info
  3. DataFrame__len__ – df.__len__
  4. DataFrame Shape – df.shape
  5. DF Count – df.count
  6. DataFrame Axis Length – (df.axes[0])

Pseudo code: Return the number of rows in a pandas DataFrame or Series

Pandas Number Of Rows

6 Methods To Find Row Count

Below are 6 methods to find out how tall your your dataset is. We’ve listed them in order of our favorite to least favorite.

DataFrame Length

len(df)

First up is DataFrame Length. This super easy and fast function will return the length of your DataFrame. The default length is the number of rows in your dataset. This is my #1 go to function to find out row count. len() come from vanilla python.

DataFrame Info

df.info()

Next is DataFrame Info. Though it is a bit slower, you’ll get more information for free. df.info() will return column names, row count, and how many non-na values you have in each row. It is useful when trying to get to know your data. I use this when I want to know row count and the characteristics of my columns.

DataFrame __len__

df.__len__

Fun fact, functions that start with double underscores have a short name of β€œdunder.” df.__len__ is a pass-through function that simply calls len(df.index). It is quick and easy. I don’t use it that often because 1) I have to type out extra characters and 2) the double underscores don’t look clean. But it’s fast!

DataFrame Shape

`df.shape[0] - To count rows

df.shape[1] - To count columns`

With DataFrame shape you’ll get the shape of your DataFrame. Yes I know that sentence is palindrome. Think of shape as the height and width of your table. You’ll be returned a tuple with two values, height and width. Shape works well, but in order to get the row count you need to reference the first item of your tuple via β€œ[0].”

DataFrame Count

df.count()

DataFrame Count will return the number of Non-NA values within each column. I don’t love this one because 1) it’s slower and 2) you need to do extra data work after your call .count(). Be careful, if you have NAs in your dataset, you may get confusing examples. .count() will skip these by default.

DataFrame Axes Length

len(df.axes[0])

Next up is our most verbose option – DataFrame Axes Length. Let’s break this one down. df.axes will return a tuple of your two axes for rows and columns. [0]will pull the first item (rows) from your tuple. Then finally len() will find the length, or how many items, you have in your axis which is your row count.

Let’s look at an examples

Link to code

On this page