How to Get the Row Count of a Pandas DataFrame?

How to Get the Row Count of a Pandas DataFrame?

Answer: You can use the df.shape attribute to count the number of rows in the pandas dataframe.

Counting rows of a pandas dataframe is an important operation in Python. You can use various pre-defined methods like count(), and len() functions to count the rows in a dataframe. In this blog, let’s explore more about these methods with the help of examples.

Table of Contents:

Before we get started with the methods, let’s create a set of DataFrame with some values inserted to make it easier to count the number of rows in a set of DataFrame.

Example:

Python

Output:

image 117

Now, we’ll use this data to count the number of rows using various pre-defined methods in Python:

Methods to Count the Number of Rows in Pandas Dataframe

Method 1: Using df.shape Attribute to Count Rows in Python  

The df.shape attribute in Pandas is used to get the dimensions of a DataFrame. It returns a tuple. 

Example:

Python

Output:

image 118

Method 2: Using len( ) Function to Count Rows in Python  

The len( ) function in Python is used to get the length of an object. When applied to a pandas dataframe, len( ) returns the number of rows in the dataframe.

Example:

Python

Output:

image 119

len( ) function is a quick way to find how many rows are in a dataframe.

Method 3: Using the .count( ) Method to Get the Row Count in Python  

The .count( ) method in Pandas counts the non-null values in each column or row of a dataframe.

Python

Output:

image 120

 df.count() method ignores any NaN (null) values and counts the non-null values in each row. If a column or row has missing (null/NaN) values, .count() will exclude those missing values from the count.

Method 4: Using Axes Attribute in Python  

The axes attribute in Pandas is used to return the list of row and column labels (indices) of a DataFrame. It provides a list where it counts the row based on the axes like axes[0], and axes[1].

Example:

Python

Output:

image 121

df.axes returns a list of two elements:

  •   Axes[0]: Row labels that indicate the index of the dataframe.
  •   Axes[1]: Column labels that indicate the columns of the dataframe.

The axes attribute is useful when you want to directly access the row and column labels or indices, especially when working with more complex dataframe tasks.

Method 5: Using index Attribute to Get Row Labels in Python  

The index attribute in Pandas gives you the row labels of a DataFrame. By default, it’s a simple range, but you can also set custom labels for the rows.

Example:

Python

Output:

image 122

Explanation: ‘df.index’ gives the index of the DataFrame. The range could be from index 0 and so on. 

It returns the row labels, which can be helpful when performing operations based on the rows.

Conclusion

By now, you have learned about the built-in methods of Python that are used to get the number of rows in a pandas dataframe. You have learned the usage of these methods with the help of examples based on a pre-defined dataframe.

 

Check out these blogs for guidance on Python’s file and module manipulation –

Rename DataFrame Columns – This article shows how to rename DataFrame columns in Python, using clear and concise examples.
Current Script Name – Curious about getting the current script name in Python? This article walks you through it with examples.
Read File Line by Line – Here’s a simple guide to reading a file line by line in Python, complete with code examples.
Get Current Date and Time – Learn to fetch the current date and time in Python through real examples shared in this article.
Delete File or Directory – Deleting files or folders in Python is made easy in this article with step-by-step examples.
Import Module from Different Directory – Need to import a module from another directory? This Python article demonstrates how with examples.
Iterate over Dictionary – Get familiar with looping through dictionaries in Python using various examples in this guide.
String Lowercase – This article covers how to convert strings to lowercase in Python, with hands-on coding examples.
NumPy where – You’ll find practical uses of NumPy’s where function in this guide, with easy-to-understand examples.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner