Missing Value Percentage per Column in a Pandas DataFrame

Quick Overview

Write a pandas function that returns the percentage of missing values in each DataFrame column, rounded to two decimals, without modifying the input. It tests boolean masks, vectorized column reductions, and care with the output's index order, dtype, name and edge cases such as an empty frame.

Missing Value Percentage per Column in a Pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `missing_percentage(df: pd.DataFrame) -> pd.Series`. For each column, return the percentage of `NaN` values (0 to 100), rounded to 2 decimal places. The function must meet these requirements: - Return a `pd.Series` indexed by column names in their original order. - Values are `float64`, rounded to 2 decimals. - The Series `name` is `None`. - Do not modify `df`. For example, a column with 1 `NaN` out of 4 rows gives 25.0: ```python df = pd.DataFrame({ "col": [1.0, None, 3.0, 4.0], "name": ["a", "b", None, None], "score": [10, 20, 30, 40], }) missing_percentage(df) # col 25.0 # name 50.0 # score 0.0 # dtype: float64 ``` ```hint Avoid counting by hand Think about what you can compute from a boolean mask of missing cells in one vectorized step, without a loop over columns or a manual count and divide. ``` ### What a Strong Answer Covers - A vectorized way to get the missing fraction of each column without modifying `df`. - The conversion to a 0 to 100 percentage and the point where rounding happens. - The shape of the returned Series: index order, dtype and name. - What counts as missing, and how the function behaves on a DataFrame with zero rows. ### Follow-up Questions - How would you also treat placeholder values such as empty strings, `"N/A"` or `-999` as missing? - What should the function return for a DataFrame with zero rows, and how would you make that behavior explicit? - How would you report only the columns above a missing-value threshold, sorted from most to least missing? - How would you compute the same percentages for a CSV file too large to load into memory at once?

Overview: Write a pandas function that returns the percentage of missing values in each DataFrame column, rounded to two decimals, without modifying the input. It tests boolean masks, vectorized column reductions, and care with the output's index order, dtype, name and edge cases such as an empty frame.

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

Implement missing_percentage(df: pd.DataFrame) -> pd.Series. For each column, return the percentage of NaN values (0 to 100), rounded to 2 decimal places.

The function must meet these requirements:

  • Return a pd.Series indexed by column names in their original order.
  • Values are float64 , rounded to 2 decimals.
  • The Series name is None .
  • Do not modify df .

For example, a column with 1 NaN out of 4 rows gives 25.0:

df = pd.DataFrame({
    "col": [1.0, None, 3.0, 4.0],
    "name": ["a", "b", None, None],
    "score": [10, 20, 30, 40],
})
missing_percentage(df)
# col      25.0
# name     50.0
# score     0.0
# dtype: float64

What a Strong Answer Covers Guidance

  • A vectorized way to get the missing fraction of each column without modifying df .
  • The conversion to a 0 to 100 percentage and the point where rounding happens.
  • The shape of the returned Series: index order, dtype and name.
  • What counts as missing, and how the function behaves on a DataFrame with zero rows.

Follow-up Questions Guidance

  • How would you also treat placeholder values such as empty strings, "N/A" or -999 as missing?
  • What should the function return for a DataFrame with zero rows, and how would you make that behavior explicit?
  • How would you report only the columns above a missing-value threshold, sorted from most to least missing?
  • How would you compute the same percentages for a CSV file too large to load into memory at once?