Pandas – Get the Row Count From DataFrame

You can get the row count of a Pandas DataFrame using len(df.index) and df.shape[0] properties. Pandas allow us to get the shape of the DataFrame by counting the number of rows in the DataFrame. DataFrame.shape property returns the rows and columns, for rows get it from the first index which is zero; like df.shape[0] and for columns count, you can get it from df.shape[1]. Alternatively, to find the number of rows that exist in a DataFrame, you can use DataFrame.count() method, but this is not recommended approach due to performance issues.

Pandas DataFrame Row Count
source: stackoverflow.com

In this article, I will explain how to count or find the DataFrame rows count with examples.

1. Pandas Get Row Count of DataFrame Examples

If you are in hurry, just glance at the below examples to try it out.


rows_count = len(df.index)
rows_count = len(df.axes[0])
rows_count = df.shape[0]
rows_count = df.count()[0]

If you are a Pandas learner, read through the article as I have explained these examples with the sample data to understand better.

2. Create a Sample DataFrame

Let’s create a Pandas DataFrame with a dictionary of lists, pandas DataFrame columns names Courses, Fee, Duration, Discount.


import pandas as pd
import numpy as np
technologies= {
    'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
    'Courses Fee' :[22000,25000,23000,24000,26000],
    'Duration':['30days','50days','30days', None,np.nan],
    'Discount':[1000,2300,1000,1200,2500]
          }
df = pd.DataFrame(technologies)
print(df)

Yields below output


   Courses  Courses Fee Duration  Discount
0    Spark        22000   30days      1000
1  PySpark        25000   50days      2300
2   Hadoop        23000   30days      1000
3   Python        24000     None      1200
4   Pandas        26000      NaN      2500

3. Get Row Count Using DataFrame.len(DataFrame.index) Method

You can use len(df.index) to find the row count of a Dataframe, df.index returns RangeIndex(start=0, stop=8, step=1) and use it on len() to get the count. You can also use len(df) but this performs slower when compared with len(df.index) since it has one less function call. Both these are faster than df.shape[0] to get the count.

If performance is not a constraint better use len(df) as this is neat and easy to read.


print(df.index)
# Outputs RangeIndex(start=0, stop=5, step=1)

print('Row count is:', len(df.index))
print('Row count is:', len(df))
# Outputs - Row count is:5

4. Get Row Count in DataFrame Using .len(DataFrame.axes[0]) Method

Pandas also provide Dataframe.axes property that returns a tuple of your DataFrame axes for rows and columns. Access the axes[0] of DataFrame and call len(df.axes[0]) to return the number of rows. For columns count, use df.axes[1]. For example: len(df.axes[1]).


print(df.axes)
# [RangeIndex(start=0, stop=5, step=1), Index(['Courses', 'Courses Fee', 'Duration', 'Discount'], dtype='object')]

print(df.axes[0])
# RangeIndex(start=0, stop=5, step=1)

print('Row count is:', len(df.axes[0]))
# Outputs - Row count is:5

5. Get Row Count of a DataFrame Using df.shape[0] Method

Pandas DataFrame.shape returns the count of rows and columns, df.shape[0] is used to get the number of rows. Use df.shape[1] to get the column count.


df = pd.DataFrame(technologies)
row_count = df.shape[0]  # Returns number of rows
col_count = df.shape[1]  # Returns number of columns
print(row_count)
#Outputs - 5

6. Get Row count DataFrame Using df.count() Method

This is not recommended approach due to its performance but, still I need to cover this as this is also one of the approaches to get the row count of a DataFrame. Note that this ignores the values from columns that have None or Nan while calculating the count. As you see, my DataFrame contains 2 None/nan values in column Duration hence it returned 3 instead of 5 on the below example.

print(df.count()) # Outputs below, count() returns count of each column Courses 5 Courses Fee 5 Duration 3 Discount 5 dtype: int64

Now let’s see how to get the row count.



rows_count = df.count()[0]
rows_count = = df[df.columns[0]].count()
print('Number of Rows count is:', rows_count )

#Outputs - Number of Rows count is: 5

Conclusion

In this article, you have learned how to find/get the pandas DataFrame row count from the DataFrame.shape of the DataFrame also, you learned to get it from len(DataFrame.index) and len(DataFrame.axes[0]), DataFrame.count() and others.

Happy Learning !!

You May Also Like

Reference

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

Pandas – Get the Row Count From DataFrame