Count Missing Values Per Column in a pandas DataFrame

Quick Overview

Implement a pandas function that returns the number of missing values in each column of a DataFrame as an int64 Series in the original column order. It tests how pandas detects NaN and None, column-wise reductions, and meeting an exact output contract without modifying the input.

Count Missing Values Per Column in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `count_missing(df)`, which returns the number of missing (`NaN`/`None`) values in each column of a pandas DataFrame. The result should be equivalent to `df.isna().sum()`. The function must meet these requirements: - Return a `pd.Series` indexed by column names in their original order. - The values are per-column null counts with dtype `int64`. - The Series `name` is `None`. - Do not modify `df`. Example: ```python df = pd.DataFrame({"a": [1, None, 3], "b": [None, None, 2]}) count_missing(df) # a 1 # b 2 # dtype: int64 ``` ```hint Comparing with NaN `NaN` is never equal to itself, so `df == np.nan` finds nothing. Look for a pandas method that detects missing values directly, then think about what adding up True/False values gives you. ``` ### What a Strong Answer Covers - A vectorized approach that counts per column without a Python loop over rows. - Which values pandas treats as missing (`NaN`, `None`, `NaT`, `pd.NA`) and why an equality test against `NaN` fails. - How the result meets the contract: original column order, `int64` dtype and a `name` of `None`. - Why the input frame stays unchanged. - Edge cases such as a frame with no rows or no columns, and duplicate column labels. ### Follow-up Questions - How would you return the share of missing values per column instead of the count, sorted from most to least missing? - The data uses empty strings and `-999` as placeholders for missing values. How would you count those too without changing `df`? - How would you count missing values per column in a CSV file too large to load into memory at once? - How would you count missing values per row, and find the rows where every value is missing?

Overview: Implement a pandas function that returns the number of missing values in each column of a DataFrame as an int64 Series in the original column order. It tests how pandas detects NaN and None, column-wise reductions, and meeting an exact output contract without modifying the input.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement count_missing(df), which returns the number of missing (NaN/None) values in each column of a pandas DataFrame. The result should be equivalent to df.isna().sum().

The function must meet these requirements:

  • Return a pd.Series indexed by column names in their original order.
  • The values are per-column null counts with dtype int64 .
  • The Series name is None .
  • Do not modify df .

Example:

df = pd.DataFrame({"a": [1, None, 3], "b": [None, None, 2]})
count_missing(df)
# a    1
# b    2
# dtype: int64

What a Strong Answer Covers Guidance

  • A vectorized approach that counts per column without a Python loop over rows.
  • Which values pandas treats as missing ( NaN , None , NaT , pd.NA ) and why an equality test against NaN fails.
  • How the result meets the contract: original column order, int64 dtype and a name of None .
  • Why the input frame stays unchanged.
  • Edge cases such as a frame with no rows or no columns, and duplicate column labels.

Follow-up Questions Guidance

  • How would you return the share of missing values per column instead of the count, sorted from most to least missing?
  • The data uses empty strings and -999 as placeholders for missing values. How would you count those too without changing df ?
  • How would you count missing values per column in a CSV file too large to load into memory at once?
  • How would you count missing values per row, and find the rows where every value is missing?