How to Count the Number of Rows in Python

Counting rows in datasets is a fundamental step in data analysis. Often, you may need to know how many rows you are dealing with, whether to understand the size of your dataset or to pre-process your data.

Two common ways we can do this in Python are using either pandas or the CSV module. With their help, you can easily count the rows in a dataset. In this tutorial, we will explore these two methods.

Example Dataset

Say you have a CSV file named ‘data.csv’ with the following content:

a,b,c,d
1,2,3,4
5,6,7,8
9,10,11,12

Method 1: Using the Pandas Library

Pandas is a high-level data manipulation tool. It is built on the Numpy package and its key data structure is called the DataFrame. The number of rows in a DataFrame is equal to the number of items in the first array.

First, you have to import the pandas library. The “pd” is a convention to shorten pandas.

Then, you read the CSV file using pandas:

Finally, you can use the built-in Python function len() to count the number of rows:

Method 2: Using the CSV Module

The CSV module in Python implements classes to read and write tabular data in CSV format. Here is how you can use it to count the number of rows.

First, import the csv module:

Next, open your file and create a csv reader object:

Finally, by simply iterating over the csv reader object, you can count the rows:

Full Code

Pandas Method:

CSV Module Method:

Conclusion

Counting the number of rows in a dataset is a common task when dealing with data in Python. You learned how to count rows using the pandas library and the csv module. You can give it a try with your own datasets too!