In this post, you’ll learn how to count the number of rows in a Pandas Dataframe, including counting the rows containing a value or matching a condition. You’ll learn why to use and why not to use certain methods (looking at you, `.count()`

!) and which methods are the fastest.

Table of Contents

## Loading a Sample Dataframe

To follow along with the tutorial below, feel free to copy and paste the code below into your favourite text editor to load a sample Pandas Dataframe that we’ll use to count rows!

```
import pandas as pd
data = {
'Level': ['Beginner', 'Intermediate', 'Advanced', 'Beginner', 'Intermediate', 'Advanced',
'Beginner', 'Intermediate', 'Advanced', 'Beginner', 'Intermediate', 'Advanced', 'Beginner',
'Intermediate', 'Advanced', 'Beginner', 'Intermediate', 'Advanced'],
'Students': [10.0, 20.0, 10.0, 40.0, 20.0, 10.0, None, 20.0, 20.0, 40.0, 10.0, 30.0, 30.0, 10.0, 10.0, 10.0, 40.0, 20.0]
}
df = pd.DataFrame.from_dict(data)
print(df.head())
```

This returns the following dataframe:

```
Level Students
0 Beginner 10.0
1 Intermediate 20.0
2 Advanced 10.0
3 Beginner 40.0
4 Intermediate 20.0
```

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

## Pandas – Number of Rows in a Dataframe

Pandas provides a lot of different ways to count the number of rows in its dataframe.

Below you’ll learn about the Pandas `len()`

function, the Pandas `.shape`

property, and the Pandas `.count()`

method.

### Pandas Len Function to Count Rows

The Pandas `len()`

function returns the length of a dataframe (go figure!). The safest way to determine the number of rows in a dataframe is to count the length of the dataframe’s index.

To return the length of the index, write the following code:

```
>> print(len(df.index))
18
```

### Pandas Shape Attribute to Count Rows

The Pandas `.shape`

attribute can be used to return a tuple that contains the number of rows and columns, in the following format `(rows, columns)`

. If you’re only interested in the number of rows (say, for a condition in a for loop), you can get the first index of that tuple.

```
>> print(df.shape[0])
18
```

### Pandas Count Method to Count Rows in a Dataframe

The Pandas `.count()`

method is, unfortunately, the slowest method of the three methods listed here. The `.shape`

attribute and the `len()`

function are vectorized and take the same length of time regardless of how large a dataframe is. The `.count()`

method takes significantly longer with with larger dataframes.

One of the benefits of the `.count()`

method is that it can ignore missing values.

```
>> print(df.count())
Level 18
Students 17
dtype: int64
```

The above output indicates that there are 18 values in the *Level* column, and only 17 in the *Students* column. This, really, counts the number of values, rather than the number of rows.

## Number of Rows Containing a Value in a Pandas Dataframe

To count the rows containing a value, we can apply a boolean mask to the Pandas series (column) and see how many rows match this condition. What makes this even easier is that because Pandas treats a `True`

as a 1 and a `False`

as a 0, we can simply add up that array.

For an example, let’s count the number of rows where the Level column is equal to ‘Beginner’:

```
>> print(sum(df['Level'] == 'Beginner'))
6
```

## Number of Rows Matching a Condition in a Pandas Dataframe

Similar to the example above, if we wanted to count the number of rows matching a particular condition, we could create a boolean mask for this.

In the example below, we count the number of rows where the Students column is equal to or greater than 20:

```
>> print(sum(df['Students'] >= 20))
10
```

## Pandas Number of Rows in each Group

To use Pandas to count the number of rows in each group created by the Pandas `.groupby()`

method, we can use the size attribute. This returns a series of different counts of rows belonging to each group.

`print(df.groupby(['Level']).size())`

This returns the following series:

```
Level
Advanced 6
Beginner 6
Intermediate 6
dtype: int64
```

To learn more about the Pandas `.groupby()`

method, check out my video tutorial here:

## Conclusion

In this post, you learned how to count the rows in a Pandas Dataframe. Specifically, you learned which methods are fastest, as well as how to count the number of rows in a dataframe containing a value, meeting a condition, and number of rows in different groups.

To learn more about the `.shape`

property, check out the official documentation here.

Pingback: Python: Split a Pandas Dataframe • datagy