Pandas | Count Number of Rows in a Dataframe

This article will discuss four ways to count the number of rows in a pandas dataframe in Python.

Table of Contents:

Let’s first create a dataframe from a list of tuples i.e.

import pandas as pd

# List of Tuples
students = [('jack', 34, 'Sydeny', 'Australia'),
            ('Riti', 30, 'Delhi', 'India'),
            ('Vikas', 31, 'Mumbai', 'India'),
            ('Neelu', 32, 'Bangalore', 'India'),
            ('John', 16, 'New York', 'US'),
            ('Mike', 17, 'las vegas', 'US')]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(  students,
                    columns=['Name', 'Age', 'City', 'Country'],
                    index=['a', 'b', 'c', 'd', 'e', 'f'])

Contents of the dataframe are,

    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US

Now let’s see different ways to count the number of rows in this dataframe.

Count the total number of rows in a Dataframe using len()

In Pandas, the dataframe has the attribute “index“, which gives an Index object containing the row index labels. We can directly call the len() function with this Index object. It will provide us with the total number of rows in the dataframe. For example,

# Get total number of rows in a Dataframe
num_of_rows = len(df.index)

print(num_of_rows)

Output:

6

As there were six rows in the dataframe, therefore we got the number 6.

Count the total number of rows in a Dataframe using shape

In Pandas, the dataframe provides an attribute “shape“. It returns a tuple representing the dimensions of the dataframe, i.e., the number of rows and columns of the dataframe. We can fetch the value at index position zero from this tuple, giving us the number of rows in the dataframe. For example

# Get total number of rows in a Dataframe
num_of_rows = df.shape[0]

print(num_of_rows)

Output:

6

As there were six rows in the dataframe, therefore we got the number 6.

Count the total number of rows in a Dataframe using the size attribute

In Pandas, the dataframe has the attribute ‘index’, which gives an Index object of row labels. We can use the ‘size‘ attribute of this index object. It will provide the total number of rows in the dataframe. For example,

# Get total number of rows in a Dataframe
num_of_rows = df.index.size

print(num_of_rows)

Output:

6

As there were six rows in the dataframe, therefore we got the number 6.

Count the total number of rows by calling len() on Dataframe object

We can directly call the len() function on a Dataframe object, and it will give us the total number of rows in the dataframe. For example,

# Get total number of rows in a Dataframe
num_of_rows = len(df)

print(num_of_rows)

Output:

6

As there were six rows in the dataframe, therefore we got the number 6.

The complete working example is as follows,

import pandas as pd

# List of Tuples
students = [('jack', 34, 'Sydeny', 'Australia'),
            ('Riti', 30, 'Delhi', 'India'),
            ('Vikas', 31, 'Mumbai', 'India'),
            ('Neelu', 32, 'Bangalore', 'India'),
            ('John', 16, 'New York', 'US'),
            ('Mike', 17, 'las vegas', 'US')]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(  students,
                    columns=['Name', 'Age', 'City', 'Country'],
                    index=['a', 'b', 'c', 'd', 'e', 'f'])

# Print the contents of the Dataframe
print(df)

print('Count Total Number of Rows in a Dataframe')

# Get total number of rows in a Dataframe
num_of_rows = len(df.index)

print(num_of_rows)

# Get total number of rows in a Dataframe
num_of_rows = df.shape[0]

print(num_of_rows)

# Get total number of rows in a Dataframe
num_of_rows = df.index.size

print(num_of_rows)

# Get total number of rows in a Dataframe
num_of_rows = len(df)

print(num_of_rows)

Output:

    Name  Age       City    Country
a   jack   34     Sydeny  Australia
b   Riti   30      Delhi      India
c  Vikas   31     Mumbai      India
d  Neelu   32  Bangalore      India
e   John   16   New York         US
f   Mike   17  las vegas         US

Count Total Number of Rows in a Dataframe
6
6
6
6

Summary:

We learned about four different ways to count the total number of rows in the dataframe.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top