get row count in pandas

How to Get Row Count of Pandas Dataframe

Python Pandas is a powerful library to organize and process data in python. It allows you to input data from files, databases and even cloud storage and store them as data tables. These data tables are called dataframes in Python and organized as rows and columns. Often you may need to get row count of Pandas dataframe. In this article, we will learn how to get row count of Pandas dataframe. Most beginners face this problem and we hope that our article helps you. There are some things to keep in mind while calculating row count in Pandas, which will describe below.


How to Get Row Count of Pandas Dataframe

Most beginners are unaware of how to get row count of Pandas Dataframe, say df, and so they often use df.count() function for this purpose. Please note, this function only returns the count of non-empty, non-NA/NAN rows and not all rows.

Here is a reliable way to get row count using df.shape object which also counts empty rows in your dataframe.

count_row = df.shape[0]  # Gives number of rows
count_col = df.shape[1]  # Gives number of columns

Here is a more concise way to get the above information.

r, c = df.shape

You can also use len function on df.index which keeps track of indexes for each row of your dataframe.

len(df.index)

In this short article, we have learnt how to get row count of pandas dataframe.

Also read:

How to Merge Dataframes in Pandas Based on Columns
How to Change Default Display Manager in Ubuntu
How to Fix ‘Please Install All Available Updates in Upgrading’
How to Change Login Screen Background in Ubuntu
Most Fdisk in Commands in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *