Skip to Content

Pandas DataFrame – Get Row Count

The size of the dataframe is a very important factor to determine the kind of manipulations and processes that can be applied to it. For example, if you have limited resources and working with large datasets, it is important to use processes that are not compute-heavy. In this tutorial, we’ll look at how to quickly get the number of rows in a pandas dataframe.

number of rows in pandas dataframe

There are a number of ways to get the number of rows of a pandas dataframe. You can determine it using the shape of the dataframe. Or, you can use the len() function. Let’s look at each of these methods with the help of an example.

First, we’ll load the rain in Australia dataset as a pandas dataframe from a locally saved CSV file.

import pandas as pd

# read the dataset 
df = pd.read_csv("weatherAUS.csv")
# display the dataframe head
df.head()

Output:

first five rows of the dataframe

You can see that the data has several features. Let’s go through some of the methods that you can use to determine the number of rows in the dataframe.

The .shape property gives you the shape of the dataframe in form of a (row_count, column_count) tuple. That is, the first element of the tuple gives you the row count of the dataframe. Let’s get the shape of the above dataframe:

# number of rows using .shape[0]
print(df.shape)
print(df.shape[0])

Output:

(145460, 23)
145460

You can see that df.shape gives the tuple (145460, 23) denoting that the dataframe df has 145460 rows and 23 columns. If you specifically want just the number of rows, use df.shape[0]

You can also use the built-in python len() function to determine the number of rows. This function is used to get the length of iterable objects. Let’s use this function to get the length of the above dataframe.

# number of rows using len()
print(len(df))

Output:

145460

We get 145460 as the length which is equal to the number of rows in the dataframe.

Note that both of the above methods, .shape[0] or len()are constant time operations and are thus pretty fast. Both involve a lookup operation and there isn’t much difference between their execution speeds so you can use either of the methods that you’re comfortable with.

Summary

In this tutorial, we looked at how to get the number of rows in a pandas dataframe. The following are the key takeaways –

  • The shape attribute of a pandas dataframe returns the (row_count, column_count) tuple. Thus, you can get the row count of a pandas dataframe from the first value of this tuple.
  • Alternatively, you can also use the Python built-in len() function to get the number of rows in a Pandas dataframe.

You might also be interested in –

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.