Skip to main content
v2026.11,986 entries · CC-BY 4.0

pandas count: DataFrame.count(), value_counts() and Missing Data

How pandas count works: DataFrame.count() for non-missing cells, value_counts() for category frequencies, and how NaN affects both, with a survey example.

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.

  1. 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],
    })
  2. Count non-missing cells per column: df.count() returns q1 4, q2 3, consent 5 (dtype int64).
  3. Count per row with df.count(axis="columns") (equivalent to axis=1): returns 3, 2, 3, 2, 2, the number of answered items per respondent.
  4. Get the total number of rows with len(df) (5) or df.shape[0]. Item response rate is then df.count() / len(df): q1 0.8, q2 0.6, consent 1.0.
  5. Count categories with df["q1"].value_counts(): Agree 3, Disagree 1. The missing value is excluded by default.
  6. 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.
  7. 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 when axis=1).
  • value_counts() returns a Series indexed by the distinct values, sorted by frequency descending. Its name is count, or proportion when normalize=True.
  • For a response-rate table, divide count() by len(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 with df.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 False to 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=False or 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about pandas count: DataFrame.count(), value_counts() and Missing Data

2 free questions a day, no account.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →