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
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.