When using Pandas dataframe to store and process your data, you may need to get a number of rows available in the dataframe.

**You can get the number of rows in the pandas dataframe using len(df.index) method.**

In this tutorial, you’ll learn how to get the number of rows in the pandas dataframe.

**If You’re in Hurry…**

You can use the `len()`

function to count the number of rows in the dataframe.

The length function returns the **length of the passed index** **or series.**

`len(df.index)`

where,

- Index means range of cells.
`df.index`

will print`RangeIndex(start=0, stop=7, step=1)`

– This will be passed to the`len()`

function to calculate the length of this range.

Using the `len()`

function is the **fastest way to count the number of rows** in the dataframe.

**Output**

`7`

**If You Want to Understand Details, Read on…**

In this tutorial, you’ll learn the different methods available to count the rows available in the pandas dataframe.

Table of Contents

## Sample Dataframe

This is the sample dataframe used throughout the tutorial.

It contains,

- rows with values
- rows with Missing data
- some duplicate rows

This can be used to demonstrate various ways of counting rows in various scenarios.

```
## pandas dataframe get row count
import pandas as pd
import numpy as np
data = {"product_name":["Keyboard","Mouse", "Monitor", "CPU", "Speakers","Speakers",pd.NaT],
"Unit_Price":[500,200, 5000, 10000, 250.50,250.50,pd.NaT],
"No_Of_Units":[5,5, 10, 20, 8,8,pd.NaT],
"Available_Quantity":[5,6,10,"Not Available", pd.NaT,pd.NaT,pd.NaT],
"Available_Since_Date":['11/5/2021', '4/23/2021', '08/21/2021','09/18/2021','01/05/2021','01/05/2021',pd.NaT]
}
df = pd.DataFrame(data)
df
```

**Dataframe Looks Like**

product_name | Unit_Price | No_Of_Units | Available_Quantity | Available_Since_Date | |
---|---|---|---|---|---|

0 | Keyboard | 500 | 5 | 5 | 11/5/2021 |

1 | Mouse | 200 | 5 | 6 | 4/23/2021 |

2 | Monitor | 5000 | 10 | 10 | 08/21/2021 |

3 | CPU | 10000 | 20 | Not Available | 09/18/2021 |

4 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |

5 | Speakers | 250.5 | 8 | NaT | 01/05/2021 |

6 | NaT | NaT | NaT | NaT | NaT |

Let’s look at the different use-cases and methods to get the number of rows in the dataframe.

There is number of ways to **get the row count** of the dataframe. Let’s discuss.

## Using Len() function

You can use the `len()`

function to get the row count in the pandas dataframe.

It accepts a single parameter as `series`

or `index`

.

You can pass the dataframe index using `df.index`

to count the number of rows in the dataframe as shown below.

**Snippet**

`len(df.index)`

**Output**

` 7`

## Using Shape() Attribute

You can also use the `shape()`

function to count the number of rows.

The `shape()`

function internally uses the `len()`

function to calculate the size of the passed index.

With `shape()`

, you can calculate the length of rows as well as columns.

Use,

`0`

to count number of rows`1`

to count number of columns

**Snippet**

`df.shape[0] `

**Output**

` 7`

You can also count **both rows and columns length** using `shape`

by just using the `shape`

property **without passing the axes**.

**Snippet**

`df.shape`

**Output**

`(7, 5)`

The output shows there are *7 rows* and *5 columns* in the dataframe.

## Using Count()

You can **count the number of rows** in the dataframe using the count() function as well.

`count()`

will not count the NA or missing values. Hence, you can use this when you want to count only the columns with non-empty values.

**Snippet**

`df[df.columns[0]].count()`

**Output**

` 7`

## Count Number of Rows With Condition

In this section, you’ll learn **how to count rows with conditions**.

**For example**, rows with values greater than a said value, or rows with values equal to the said value, and so on.

### Count Rows Based On Column Value

You can **count rows based on column value** by specifying the column value and using the shape attribute.

In the below example, you’re calculating the number of rows where the *Unit_Price* is greater than 1000.

**Snippet**

`df[df.Unit_Price > 1000].shape[0]`

There are **two values** in the Unit_Price column which is **greater than 1000**. Hence you’re seeing the output `2`

.

**Output**

` 2`

This is how you can count rows based on column value.

Next, you’ll get a row count between two values.

### Count Rows Between Two Values

In this section, you’ll **count the number of rows with values** which is between the **range of two values.**

In the example below, you’re counting the number of rows where the *unit_price* is between `1000`

and `6000`

.

**Snippet**

`df[(df.Unit_Price > 1000) & (df.Unit_Price > 6000)].shape[0]`

There is one value in the Unit_Price column that is greater than 1000 and less than 6000. Hence you’ll see the output as `1`

.

**Output**

` 1`

This is how you can count rows between two values.

Next, let’s see about the two equal columns.

### Count Rows Where Two Columns Are Equal

In this section, you’ll **count rows where two columns are equal.**

In the example, you’re counting the number of rows where the columns *No_of_units* and *Available_Quantity* have equal values.

**Snippet**

`df[(df.No_Of_Units) == (df.Available_Quantity)].shape[0]`

There are two rows in the dataframe where the columns *No_of_units* and *Available_Quantity* have equal values. Hence, you’ll see the output as `2`

.

**Output**

` 2`

This is how you can count rows with two columns having equal value.

Next, let’s see about counting rows with a specific value.

### Count Specific Value in a Row

In this section, you’ll learn how to **count rows that have a specific value in columns**. You can do this by specifying the condition in the dataframe and using the shape attribute.

In the example, you’re counting the number of rows where the column *No_Of_Units* is having the value `5`

.

**Snippet**

`df[(df.No_Of_Units) == 5].shape[0]`

There are two rows in the sample dataframe where the column *No_Of_Units* is having the value `5`

. Hence you’ll see the output `2`

.

**Output**

` 2`

This is how you can count rows where the column has a specific value.

## Conclusion

To summarize, you’ve learned how to get the number of rows in the dataframe.

You’ve used different methods such as `len()`

, `shape[]`

, and also `count()`

to count the values in rows based on different use-cases.

If you have any questions, comment below.