Written and maintained by CASRAI Editorial Board
Published
On this page
pandas count usually means one of two things: DataFrame.count(), which counts non-missing cells per column or row, and Series.value_counts(), which counts how often each distinct value occurs. This guide shows both on a small survey-style table with missing answers, so you can report item response counts and category frequencies correctly. It is written for researchers and analysts who work in Python (the same task in R is covered in how to make a frequency table).
What pandas count is used for in research
DataFrame.count() answers “how many usable answers does each variable have?” The pandas documentation defines it as counting non-NA cells for each column or row, where None, NaN, NaT and pandas.NA are all treated as NA. Series.value_counts() answers “how many respondents chose each category?” Typical research uses are item response counts per survey question, response rates, category frequencies, and checking how much missing data each variable has before analysis (see also pandas isna for detecting the missing cells themselves).
Install or access
The official pandas installation page gives pip install pandas and conda install -c conda-forge pandas. Both methods are listed at pandas.pydata.org/docs/getting_started/install.html. Then import it with import pandas as pd. For exploratory work in a broader pipeline, see Python for exploratory data analysis.
Step by step: count responses in a survey table
The example table has five respondents. Question q1 (text) is missing for respondent 3, and q2 (a 1–5 rating) is missing for respondents 1 and 4. Counts below were produced by running the code in pandas 3.0.
- Build the data.
import pandas as pd import numpy as np df = pd.DataFrame({ "q1": ["Agree", "Disagree", "Agree", None, "Agree"], "q2": [4, np.nan, 3, 5, np.nan], "consent": [True, True, False, True, True], }) - Count non-missing cells per column:
df.count()returns q1 4, q2 3, consent 5 (dtype int64). - Count per row with
df.count(axis="columns")(equivalent toaxis=1): returns 3, 2, 3, 2, 2, the number of answered items per respondent. - Get the total number of rows with
len(df)(5) ordf.shape[0]. Item response rate is thendf.count() / len(df): q1 0.8, q2 0.6, consent 1.0. - Count categories with
df["q1"].value_counts(): Agree 3, Disagree 1. The missing value is excluded by default. - Get proportions with
df["q1"].value_counts(normalize=True): Agree 0.75, Disagree 0.25. Note that proportions use the 4 answered cells as the denominator, not all 5 respondents. - Keep missing values visible with
df["q1"].value_counts(dropna=False): Agree 3, Disagree 1, NaN 1.
Signatures and arguments
| Call | Signature (pandas 3.1.1 docs) | Key arguments |
|---|---|---|
DataFrame.count |
count(axis=0, numeric_only=False) |
axis: 0 or ‘index’ counts per column; 1 or ‘columns’ counts per row. numeric_only: include only float, int or boolean data. |
Series.value_counts |
value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True) |
normalize: relative frequencies. sort: stable sort by frequency (stable since pandas 3.0.0). bins: half-open bins for numeric data, a convenience for pd.cut. dropna: set False to include NaN counts. |
Series.count() works the same way on a single column. df.count(numeric_only=True) on the example drops q1 (text) and keeps q2 (3) and consent (5), because boolean columns are included.
Which function counts what
| Need | Use | Missing values |
|---|---|---|
| Non-missing cells per column or row | df.count() |
Not counted |
| Number of rows (and columns) | len(df), df.shape |
Rows with missing cells still counted |
| Missing cells per column | df.isna().sum() |
Counted (q1 1, q2 2, consent 0 in the example) |
| Frequency of each distinct value | s.value_counts() |
Excluded unless dropna=False |
| Bin numeric values and count | s.value_counts(bins=3) (see pandas cut for custom bins and labels) |
Excluded |
The pandas documentation lists Series.count, DataFrame.value_counts, DataFrame.shape and DataFrame.isna as related to DataFrame.count. df.value_counts(["q1", "consent"]) counts unique row combinations of the listed columns, giving Agree/True 2, Disagree/True 1, Agree/False 1 in the example; rows with a missing value in a listed column are dropped by default.
Output and how to read it
count()returns a Series of integers indexed by column name (or row label whenaxis=1).value_counts()returns a Series indexed by the distinct values, sorted by frequency descending. Its name iscount, orproportionwhennormalize=True.- For a response-rate table, divide
count()bylen(df), not by the largest column count, unless that is the denominator your protocol defines.
Troubleshooting
- A column shows a higher count than expected. Only None, NaN, NaT and pandas.NA are NA. Empty strings (
""), spaces and codes such as"999"or"N/A"text are counted as real values. Convert them first, for example withdf.replace("", np.nan). - Counts differ between columns. This is expected:
count()is computed separately per column, so it reflects each variable’s missing data, not the number of respondents. - A category is missing from value_counts. NaN is dropped by default; use
dropna=False. - A column is absent from df.count(numeric_only=True). Text columns are excluded by that argument; leave it at the default
Falseto count them. - Tied categories change order between versions. The pandas docs record that the sort in value_counts became stable in 3.0.0; pass
sort=Falseor sort the index yourself if your report needs a fixed order.
Free alternatives
| Tool | Equivalent of | Note |
|---|---|---|
| R, base | table(x) |
Frequency table of one or more factors; see how to make a frequency table |
| R, dplyr | count(df, q1) |
Counts rows per group |
| SQL | COUNT(column) / COUNT(*) |
COUNT(column) ignores NULLs; COUNT(*) counts rows |
FAQ
What does count do in pandas?
DataFrame.count() returns, for each column (or row with axis=1), the number of cells that are not NA. It does not count rows; use len(df) for that.
How do I count values in a pandas column?
Use df["column"].value_counts() for the frequency of each distinct value, or df["column"].count() for the number of non-missing entries.
What is the difference between count and value_counts?
count() gives one number per column (non-missing cells); value_counts() gives one number per distinct value in a Series.
How do I count missing values in pandas?
Use df.isna().sum(), or subtract df.count() from len(df).
Does count include NaN?
No. None, NaN, NaT and pandas.NA are all excluded from count().
Sources
- pandas documentation, DataFrame.count (version 3.1.1 docs, read 2026-11-08).
- pandas documentation, Series.value_counts (version 3.1.1 docs, read 2026-11-08).
- pandas documentation, Installation (read 2026-11-08).







