Pandas Get the Number of Rows

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:October 30, 2022

You can get the number of rows in Pandas DataFrame using len(df.index) and df.shape[0] properties. Pandas allow us to get the shape of the DataFrame by counting the number of rows in the DataFrame.

DataFrame.shape property returns the rows and columns, for rows get it from the first index which is zero; like df.shape[0] and for columns count, you can get it from df.shape[1]. Alternatively, to find the number of rows that exist in a DataFrame, you can use DataFrame.count() method, but this is not recommended approach due to performance issues.

Pandas DataFrame Row Count

In this article, I will explain how to count or find the DataFrame rows count with examples.

1. Quick Examples of Get the Number of Rows in DataFrame

If you are in hurry, below are some quick examples of how to get the number of rows (row count) in pandas DataFrame.

rows_count = len(df.index)
rows_count = len(df.axes[0])
rows_count = df.shape[0]
rows_count = df.count()[0]

If you are a Pandas learner, read through the article as I have explained these examples with the sample data to understand better.

Let’s create a Pandas DataFrame with a dictionary of lists, pandas DataFrame columns names Courses, Fee, Duration, Discount.

import pandas as pd
import numpy as np
technologies= {
    'Courses Fee' :[22000,25000,23000,24000,26000],
    'Duration':['30days','50days','30days', None,np.nan],
df = pd.DataFrame(technologies)

Yields below output

   Courses  Courses Fee Duration  Discount
0    Spark        22000   30days      1000
1  PySpark        25000   50days      2300
2   Hadoop        23000   30days      1000
3   Python        24000     None      1200
4   Pandas        26000      NaN      2500

2. Get Number of Rows in DataFrame

You can use len(df.index) to find the number of rows in pandas DataFrame, df.index returns RangeIndex(start=0, stop=8, step=1) and use it on len() to get the count. You can also use len(df) but this performs slower when compared with len(df.index) since it has one less function call. Both these are faster than df.shape[0] to get the count.

If performance is not a constraint better use len(df) as this is neat and easy to read.

# Outputs RangeIndex(start=0, stop=5, step=1)

print('Row count is:', len(df.index))
print('Row count is:', len(df))
# Outputs - Row count is:5

3. Get Row Count in DataFrame Using .len(DataFrame.axes[0]) Method

Pandas also provide Dataframe.axes property that returns a tuple of your DataFrame axes for rows and columns. Access the axes[0] and call len(df.axes[0]) to return the number of rows. For columns count, use df.axes[1]. For example: len(df.axes[1]).

# [RangeIndex(start=0, stop=5, step=1), Index(['Courses', 'Courses Fee', 'Duration', 'Discount'], dtype='object')]

# RangeIndex(start=0, stop=5, step=1)

print('Row count is:', len(df.axes[0]))
# Outputs - Row count is:5

4. Using df.shape[0] to Get Rows Count

Pandas DataFrame.shape returns the count of rows and columns, df.shape[0] is used to get the number of rows. Use df.shape[1] to get the column count.

df = pd.DataFrame(technologies)
row_count = df.shape[0]  # Returns number of rows
col_count = df.shape[1]  # Returns number of columns
#Outputs - 5

5. Using df.count() Method

This is not recommended approach due to its performance but, still I need to cover this as this is also one of the approaches to get the row count of a DataFrame. Note that this ignores the values from columns that have None or Nan while calculating the count. As you see, my DataFrame contains 2 None/nan values in column Duration hence it returned 3 instead of 5 on the below example.

print(df.count()) # Outputs below, count() returns count of each column Courses 5 Courses Fee 5 Duration 3 Discount 5 dtype: int64

Now let’s see how to get the row count.

rows_count = df.count()[0]
rows_count = = df[df.columns[0]].count()
print('Number of Rows count is:', rows_count )

#Outputs - Number of Rows count is: 5


In this article, you have learned how to find/get the number of rows (simply row count) in DataFrame by using DataFrame.shape of the DataFrame also, you learned to get it from len(DataFrame.index) and len(DataFrame.axes[0]), DataFrame.count() and others.

Happy Learning !!


NNK is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Pandas Get the Number of Rows