HOWTO · Pandas

How to Get the Row Count of a Pandas DataFrame

Learn to count rows in a Pandas DataFrame with shape, len(), its index, and boolean conditions, while distinguishing rows from columns.

On this page

To get the number of rows in a Pandas DataFrame, use df.shape[0] or len(df). The shape attribute returns both dimensions: its first value is the row count, and its second is the column count. To count only rows that meet a condition, sum a Boolean mask.

Using DataFrame.shape to Get Row and Column Counts

DataFrame.shape is an attribute that returns a tuple in the order (rows, columns). Select index 0 for the row count and index 1 for the number of columns. This is useful when you need to inspect both dimensions or want the row count directly without counting values in a particular column.

The following example builds a small DataFrame and prints both dimensions:

import pandas as pd

data = {
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["New York", "Los Angeles", "Chicago"],
}
df = pd.DataFrame(data)

print("Rows:", df.shape[0])
print("Columns:", df.shape[1])

For the row count alone, df.shape[0] is the relevant expression. Avoid using df.shape[1] for this task: it reports columns, so a table with three rows and three columns would make the two counts look the same.

Using len() or the Index to Get the Row Count

Python’s built-in len() function returns the number of rows when passed a DataFrame: len(df). It is a concise choice when you only need the row count. len(df.index) gives the same count by measuring the DataFrame’s row index, while len(df.axes[0]) accesses the length of the row axis. For the column count, len(df.axes[1]) corresponds to df.shape[1].

These forms count rows regardless of the index labels; they do not count non-missing values in a selected column. To count rows that contain no missing values, first filter the data with df.dropna() and then use len(df.dropna()). By default, dropna() removes rows containing a missing value in any column; pass subset=["Age"] to consider only a particular column.

There is no need to call .len() on a DataFrame: len() is a Python function, so write len(df) rather than df.len() or .len(df.index). Use the same DataFrame in the count expression that you want to measure, such as a filtered DataFrame.

Counting Rows That Match a Condition

Create a Boolean mask from a column comparison, then call .sum() on that mask. In a Boolean Series, each True contributes one and each False contributes zero, so the sum gives the number of matching rows. This vectorized approach is straightforward for a single comparison and can be extended to several conditions.

import pandas as pd

df = pd.DataFrame({"Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35]})
is_over_28 = df["Age"] > 28
matching_rows = is_over_28.sum()
print(matching_rows)

For multiple comparisons, put each comparison in parentheses and combine them with & for AND or | for OR. For example, (df["Age"] > 28) & (df["Age"] < 35) counts rows whose age falls within both limits; sum the resulting mask to get the count. Do not use Python’s and or or between whole Pandas Series.

To count distinct rows, remove duplicates before counting: len(df.drop_duplicates()). This answers a different question from counting every row, because repeated records contribute only once after drop_duplicates(). Choose the expression based on whether you need all rows, rows matching a filter, or distinct records.