Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To count DataFrame rows that meet a condition, build a Boolean mask and sum it: count = int(mask.sum()). For example, mask = df["score"].ge(80) counts rows with scores of at least 80. If you also need the matching records, filter with df.loc[mask] and count them with len(...) or .shape[0].

Count rows that meet one condition

A comparison such as df["Age"] > 35 returns a Boolean Series with one value per DataFrame row. Use that Series as a mask, then sum its true values:

mask = df["score"].ge(80)
count = int(mask.sum())

The int() conversion makes the result a regular Python integer. You can also filter the DataFrame and count the resulting rows:

matching_rows = df.loc[mask]
count = len(matching_rows)
# Equivalent:
count = matching_rows.shape[0]

Summing the mask is compact when you only need the count. Filtering first is useful when you also want to inspect, export, or process the matching rows.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Combine multiple conditions

Use & for AND, | for OR, and ~ to negate a condition. Put parentheses around each comparison so Python evaluates the conditions as intended.

Require both conditions

mask = (df["age"] >= 18) & (df["country"] == "US")
count = int(mask.sum())

Match either condition

mask = (df["status"] == "active") | (df["priority"] == "high")
count = len(df.loc[mask])

Exclude a condition

mask = ~(df["status"] == "cancelled")
count = int(mask.sum())

Match any value in a list

Use .isin() when a row qualifies if a column contains one of several values:

mask = df["status"].isin(["active", "pending"])
count = int(mask.sum())

Count rows by group or value

For a count per group, filter to qualifying records and use GroupBy.size(). It counts rows in each group, including rows where other columns have missing values.

mask = df["score"].ge(80)
counts = df.loc[mask].groupby("department").size()

Choose the operation according to what you mean by “count”:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
What you want to count Use How missing values are handled
Rows meeting a condition int(mask.sum()), len(df.loc[mask]), or df.loc[mask].shape[0] The mask has one Boolean value per row.
Rows in each group df.groupby("category").size() Counts group rows, even if other columns contain missing values.
Non-missing values per group and column df.groupby("category").count() Counts non-missing values separately in each column.
Frequency of each value in one column df["category"].value_counts() By default, missing values are omitted; use dropna to control this.
Frequency of unique row combinations df.value_counts(subset=["a", "b"], dropna=False) By default, combinations containing missing values are omitted; dropna=False includes them.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Understand missing values and count behavior

Comparisons involving missing values do not make those rows true matches. If the condition is that a value is missing, test it explicitly:

mask = df["col"].isna()
count = int(mask.sum())

DataFrame.count() is not a general row counter: it counts non-missing cells, by column by default, and excludes None, NaN, NaT, and pandas.NA. To get the DataFrame’s dimensions regardless of missing entries, use df.shape; its first element is the number of rows.

Rank #4
Sale
Pandas Journal (Diary, Notebook)
  • Crisp writing pages are perfect for personal reflections, sketching, or for recording favorite quotations or poems.
  • Premium 120 gsm paper takes pen or pencil beautifully.
  • Paper is acid free and of archival quality.
  • Light gray lines subtly guide your writing.
  • An inside back cover pocket expands to hold notes, cards, mementos, and more.
row_count = df.shape[0]

For a Series sum, pandas returns zero by default for an empty or all-missing Series. If the intended meaning is “no valid count available” rather than zero, min_count=1 makes the sum return a missing value when there are no valid values:

total = series.sum(min_count=1)

These API descriptions and examples are documented in the pandas DataFrame.count documentation, indexing and selection guide, Boolean indexing guide, pandas comparison with SQL guide, GroupBy.count documentation, Series.value_counts documentation, DataFrame.value_counts documentation, and Series.sum documentation. The cited API pages were surfaced as pandas 3.0.6 documentation; check the documentation for your installed pandas version if version-specific behavior matters.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

SaleBestseller No. 4
Pandas Journal (Diary, Notebook)
Pandas Journal (Diary, Notebook)
Premium 120 gsm paper takes pen or pencil beautifully.; Paper is acid free and of archival quality.
$10.99
Bestseller No. 5
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
Funny design. Import pandas as pd, an all too familiar python code.; Lightweight, Classic fit, Double-needle sleeve and bottom hem
$19.99
Best Value
Pandas Funny GIS/Programming/Python T-Shirt, Men, Black, Small
  • Funny design. Import pandas as pd, an all too familiar python code.
  • Featuring a familiar python code, this will get a laugh from all the nearby programmers and GIS professionals.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.