DataFrame is the 2-dimensional array in Pandas library consisting of column names and index labels. However, when transforming data in another format, the 2-D shape of the dataframe (2,3) is transformed to a 1-D shape (6) for better understanding and training of the model. In such scenarios, the tuple representation of the dimensionality (rows, columns) of the DataFrame gives the programmer an instance idea of how many features and labels, they are working with.

This article is about how you get the row count of a Pandas DataFrame. The following topics will be covered in this article:

- How To Get The Row Count Of a Pandas DataFrame?
- Count the RangeIndex In Pandas DataFrame
- Get the Total Row Count in Pandas DataFrame
- Fetch the Shape in Pandas DataFrame
- Using “Index” Attribute in Pandas DataFrame
- Using “axes” in Pandas DataFrame
- Using “count()” Function in Pandas DataFrame
- Using “groupby()” in Pandas DataFrame to Fetch the Row Count
- Fetch the Row Count by Defining a Function in Pandas DataFrame

**How To Get The Row Count Of a Pandas DataFrame?**

The shape of the DataFrame gives information about the count of rows and columns in the dataset. However, knowing about the row and column count gives flexible access to reshape data according to the predicted model.

To get the information about the number of rows in pandas DataFrame, you need to import the dataset to the IDE. Here’s how you can load the dataset in Pandas to the Python script:

**Importing Dataset To The Python Script**

First, load the dataset in “**CSV**” format (in our case) using the “**read_csv()**” function. This will convert it to the pandas DataFrame. Here’s how you can load the dataset in the pandas:

```
#import the pandas library to the Python script
#use pnd as a shorthand name
import pandas as pnd
#reading dataset with pandas library
df = pnd.read_csv ('tested.csv')
#head() displays the first 5 rows from the dataset on the console
df.head()
```

**Note:** The “**head()**” function displays the first 5 rows from the DataFrame as an output.

**Output**

The DataFrame is displayed on the output, showing the first 5 rows:

**Approach 1: Count the RangeIndex In Pandas DataFrame**

To get the information about the row count of the Pandas DataFrame the user can use the “**info()**” function in Python. To get the row count utilize the “**info()**” function with DataFrame using the dot(.) operator. The **info()** function will return the count of “**RangeIndex**”, the column count, and other descriptive information on the output:

`df.info()`

**Output**

The output shows that the total “**row** **count**” of the loaded DataFrame has **418 **entries, starting from **0-417**:

**Approach 2: Get the Total Row Count in Pandas DataFrame**

The “**len()**” function in the Pandas DataFrame can also be utilized to get the total row count of the dataset. To do so, you need to pass the DataFrame as an argument to the len() function:

```
rows_count=len(df)
rows_count
```

**Output**

The below snap shows the output of the DataFrame returning the information about the total number to rows:

**Approach 3: Fetch the Shape in Pandas DataFrame**

The row count of a dataset can be fetched by knowing the shape of the Pandas DataFrame. However, the shape attribute in Python gives flexible access to users to extract information about the dimensionality of the DataFrame. The dimensionality of a 2-D Dataframe is the count of rows and columns in the dataset.

To get the row count of a Pandas DataFrame you will need to invoke a “**dataframe** **variable**” (df) with the “**shape**” attribute using the **dot(.)** operator:

`rows_count=df.shaperows_count`

**Output**

The above code returns the dimensionality(rows, columns) of the Dataframe. In our case, the DataFrame has 418 rows and 12 Columns in total:

**Extract The Row Count **

Another approach to get the row count using the shape attribute is by embedding the “**0**” within the square braces “**[ ]**” at the end of the constructed variable (“**row_count**”). This leading zero will be utilized to extract only row count information from the DataFrame using the shape attribute:

```
rows_count=df.shape
rows_count[0]
```

**Output**

Adding a zero at the end of the variable will extract the first element. Here, in our case, the shape size is (**418,12**). Here index “**0**” represents the “**row** **count**”, and index “1” represents the column count. Adding the leading **[0]** at the end returns the **first index** information:

**Another Prevalent Approach To Add Leading [0] To Get The Row Count**

As mentioned in the above section, the leading zero can be added with the shape attribute to get the row count information only. Here’s how you can do this:

```
rows_count=df.shape[0]
rows_count
```

**Output**

The shape attribute returns only the row count from the tuple (rows, columns):

**Approach 4: Using “Index” Attribute in Pandas DataFrame**

Indexing is the prevalent approach in Python to fetch the particular element/item from the sequence. Python gives flexible access to the elements index-wise from DataFrame using the “**index**” attribute. The “index” property in the DataFrame is used to get the index label information. In the Dataframe the index label represents the number of rows in the dataset.

Here’s how you can get the row count using the index attribute:

```
rows_count=df.index
rows_count
```

**Output**

The “**index**” returns the index label of the DataFrame which is “**418**” in our case:

**Using “size” Attribute With “index” Property To Get The Row Count**

The row count details can be fetched from the Pandas DataFrame by using the “**index**” property with the “**size**” attribute in Python. Doing so returns the direct row count on the output of the DataFrame:

```
rows_count=df.index.size
rows_count
```

**Output**

The output returns the row count (418) from the particular DataFrame:

**To Get The Row Count Using “index” Attribute With “len()” Function **

To get the row count of a Pandas DataFrame, the user can utilize the “**len()**” function along with the “**index**” attribute. Pass the “**index**” attribute within the “**len()**” parenthesis as an argument. The blow example code is another way to access the row count of a Pandas DataFrame:

```
rows_count=len(df.index)
rows_count
```

**Output **

The above example code, fetched the row count of a Pandas DataFrame using the “**len(dataframe.index)**” function:

**Approach 5: Using “axes” in Pandas DataFrame**

Utilizing the “**dataframe.axes**” returns the information about the dimensionality of the Dataframe in Pandas. Embedding **“[0]”** zero at the end of the attribute will display the information about the index label of the DataFrame. And using the “**size**” attribute to display only the row count on the output console, discarding the other undesired information of the DataFrame:

```
rows_count=df.axes[0].size
rows_count
```

**Output**

The below snap returns the total row count of the Pandas DataFrame using the “**axes**” attribute:

**Using The “len()” Function With the “axes” Attribute To Get the Row Count**

To get the row count of the Pandas DataFrame use the “len()” function along with the “**axes[0]**” attribute in Python. The leading “**[0]**” embedded at the end will return only the first element information from the sequence, which is row count:

```
rows_count = len(df.axes[0])
rows_count
```

**Output **

The below snap is the output of using the “**len(df.axes[0])**” to get the row count of a Pandas DataFrame:

**Approach 6: Using “count()” Function in Pandas DataFrame**

To get the row count of a Pandas DataFrame the user can utilize the build-in “**count()**” function in Python. The “**count()**” function gives flexible access to users to get the “**row count**” of a Pandas DataFrame. However, the count() function counts the non-empty values in the rows and returns the count of the particular rows from the DataFrame features (column names):

```
rows_count = df.count()
rows_count
```

**Output**

The below snap is the output of the “count()” function that counts the rows count each feature in the Dataframe:

**Approach 7: Using “groupby()” in Pandas DataFrame to Fetch the Row Count**

The “**groupby()**” function is used for a large amount of particular data or features and applies the operation on it according to the preference. The count of the rows in Pandas DataFrame can be fetched using the “**groupby()**” function along with the “**size()**” function.

In our case, the goal is to extract the row count of the particular feature “**PassengerId**” from the DataFrame. However, using the “**size()**” function with the “**groupby()**” function to get the count of rows of the column name “**PassengerId**”:

`df.groupby('PassengerId').size()`

**Output**

The below snap illustrates the length of the rows for a particular column named “**PassengerId**”:

**Approach 8: Fetch the Row Count by Defining a Function in Pandas DataFrame **

The user-defined function gives flexible access to users to perform the task and create a logical statement within a “**def**” code body. However, utilizing the def function to get the row count of a Pandas DataFrame. To do so, pass the shape() function with the addition of leading zero “**[0]**” to the created dataframe (df) using the dot(.) operator. The embedded leading zero [0] will return only the row count by accessing the first element from the sequence:

```
def rows_count(df):
dataframe= df.shape[0]
print(f"\nCheck for row count in a dataset using pandas: \n{dataframe}")
rows_count(df)
```

**Output**

The below snap is returning the row count of a Pandas DataFrame:

**Note: **The user can implement the above example code in the following method by using the string literal “**format()**” function. The format() function formats the passed value to it within parentheses”**()**” and inserts them into the placeholders that are enclosed with the quotes and curly braces “**{}**”.

```
def rows_count(df):
print("{:,}".format(df.shape[0]))
rows_count(df)
```

**Output**

This article is all about getting the row count of a Pandas DataFrame.

**Conclusion**

To get the row count of a Pandas DataFrame, first, construct the DataFrame, or load the dataFrame into the IDE. The user can utilize the built-in Python functions and attributes to fetch the number of rows of a Pandas Dataframe. This function includes, “**info()**”, “**shape()**”, “**len()**”, “**index**”, and “**size()**”. However, the straightforward approach is to use the “**count()**” function to get the row count of a DataFrame. This article has demonstrated the approaches to get the row count of a Pandas DataFrame.