The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Create a Boolean mask from the condition, then sum it to count matching rows. If you also need the matching records, filter the DataFrame and count the result.
Count rows that meet one condition
A comparison on a DataFrame column produces a Boolean Series with one value for each row. For example, to count rows where score is at least 80:
As an Amazon Associate I earn from qualifying purchases.
mask = df["score"].ge(80)
count = int(mask.sum())
The mask contains True for matching rows and False otherwise. Summing it counts the true values; wrapping the result in int() gives you a regular Python integer.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →If you want the filtered rows as well as their count, use the same mask to select them:
#1 Best Overall
matching_rows = df.loc[mask]
count = len(matching_rows)
# Equivalent:
count = matching_rows.shape[0]
Boolean indexing keeps rows where the condition is true. See pandas’ selection and indexing guide.
Combine conditions
Use & for AND, | for OR, and ~ for NOT. Put parentheses around each comparison so Python evaluates the comparisons before combining their masks.
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())
For a column that should match one of several values, use .isin():
mask = df["country"].isin(["US", "CA"])
count = int(mask.sum())
The pandas indexing guide covers selecting rows by conditions and values.
Rank #3
Count matching rows within each group
To count qualifying records by category, filter first and then use groupby(...).size():
mask = df["score"].ge(80)
counts = df.loc[mask].groupby("department").size()
size() counts rows in each group, including rows whose other columns contain missing values. This makes it the appropriate choice when the question is how many records are in each group.
Rank #4
- 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.
Choose the right counting method
| Question | Use | What it counts |
|---|---|---|
| How many rows match a condition? | int(mask.sum()), len(df.loc[mask]), or df.loc[mask].shape[0] |
Rows selected by the Boolean mask. |
| How many non-missing values are in each column? | df.count() |
Non-NA cells in each column, not total rows. It excludes None, NaN, NaT, and pandas.NA. See the DataFrame.count documentation. |
| How many non-missing values are in each row? | df.count(axis="columns") |
Non-NA cells per row. |
| How many rows are in each group? | df.groupby("category").size() |
Rows per group, regardless of missing values in other columns. |
| How many non-missing values per group and column? | df.groupby("category").count() |
Non-NA values in each column, separately for each group. |
| How often does each value in one column appear? | df["category"].value_counts() |
Frequency of each value; set dropna to control how missing values are handled. |
| How often does each distinct combination of columns appear? | df.value_counts(subset=["a", "b"], dropna=False) |
Frequency of unique row combinations. By default, combinations containing NA are omitted. |
For SQL-style group record counts, pandas’ comparison with SQL uses groupby("sex").size() and explains why count() gives a different result.
Handle missing values and empty results
A comparison involving a missing value does not make that row a true match. If missingness is the condition you want to count, test for it explicitly:
Best Value
- 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
mask = df["col"].isna()
count = int(mask.sum())
Use df.shape[0] when you need the DataFrame’s total row count regardless of missing entries. Unlike DataFrame.count(), it does not count non-missing cells.
For frequency counts, check the missing-value defaults: DataFrame.value_counts() omits combinations containing NA unless you pass dropna=False, while a column’s Series.value_counts() also has a dropna option. See the DataFrame.value_counts documentation and Series.value_counts documentation.
If you are summing numeric data rather than a Boolean mask, an empty or all-NA Series sums to zero by default. Set min_count=1 when you want an empty or all-NA input to produce NA instead of zero; the Series.sum documentation describes this behavior.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
The linked API pages are from the pandas 3.0.6 documentation surfaced for this topic. If your project depends on version-specific behavior, consult the documentation for its installed pandas version.
Quick Recap
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.




