This article will discuss four ways to count the number of rows in a pandas dataframe in Python.
Table of Contents:
- Get total number of rows using len() function with Dataframe.Index.
- Get total number of rows using shape property.
- Get total number of rows using size property.
- Get total number of rows using len() on dataframe object.
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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.