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.
How to get the number of rows in a 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()
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.
Method 1 – Get row count using
.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 print(df.shape) print(df.shape)
(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
Method 2 – Get row count using the
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))
We get 145460 as the length which is equal to the number of rows in the dataframe.
Note that both of the above methods,
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.
In this tutorial, we looked at how to get the number of rows in a pandas dataframe. The following are the key takeaways –
shapeattribute 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 –
- Pandas – Get DataFrame Size (With Examples)
- Pandas – Get Value of a Cell in Dataframe
- Pandas – Create DataFrame Copy
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.