How to get the number of rows in DataFrame?

Given a DataFrame, we need to find the total number of rows in this DataFrame.
Submitted by Pranit Sharma, on April 10, 2022

Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas, we can also assign index names according to the needs. To get the total number of rows, we have DataFrame.shape[0] method.

pandas.DataFrame.shape[0] Method

This method usually works without an index and it is used to find the total number of rows and columns in a DataFrame, but to find the number of rows specifically, 0 is given as an index. Also, to find the number of columns, 1 is given as an index.


# Syntax for both rows and columns

# Syntax for rows

To work with Python Pandas, we need to import the pandas library. Below is the syntax,

import pandas as pd

Let us understand that how to get the number of rows in a DataFrame with the help of an example?

# Importing pandas package

import pandas as pd

# Creating a dictionary of student marks

d = {

# Now, we will create DataFrame and 
# we will assign index name as subject names
df = pd.DataFrame(d,index=["Maths","Physics","Chemistry","English"])

# Printing the DataFrame

# Using df.shape[0] method to 
# get the number of rows directly
print("Total number of rows are:",df.shape[0])


           Peter  Harry  Tom  John
Maths         65     45   67    56
Physics       70     56   87    78
Chemistry     70     66   65    65
English       75     66   53    64 

Total number of rows are: 4


Here, we have created a DataFrame with four rows having index names Maths, Physics, Chemistry, and English respectively. df.shape[0] method will return the count of the number of rows hence the output is 4.

Python Pandas Programs »



Comments and Discussions!





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.