How do I get the row count of a Pandas DataFrame?
You can use the shape property of the DataFrame to get the number of rows and columns.
You can use the shape property of the DataFrame to get the number of rows and columns. The shape property returns a tuple with the number of rows as the first element and the number of columns as the second element. To get the number of rows, you can do:
Get the number of rows using the shape property
import pandas as pd
df = pd.read_csv('file.csv')
num_rows = df.shape[0]
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
Alternatively, you can use the len function to get the number of rows:
Get the number of rows using the len function
num_rows = len(df)You can also use len(df.index) for the same result.
Alternatively, you can use the count method combined with sum to get the total number of rows:
Get the number of rows using count and sum
num_rows = df.count().sum()Note: This method counts non-null values across all columns. It will only equal the row count if there are no missing values, and it is generally slower than shape or len for large DataFrames.