Column-Wise Z-Score of Numeric DataFrame Columns With ddof=0

Quick Overview

Write a pandas function that standardizes every numeric column of a DataFrame with the population standard deviation and passes all other columns through unchanged. It tests dtype-based column selection, the pandas ddof default, copying without mutation, and handling of constant columns and missing values.

Column-Wise Z-Score of Numeric DataFrame Columns With ddof=0

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement `zscore_numeric`, which returns a new DataFrame where each numeric column is standardized as `(col - col.mean()) / col.std(ddof=0)` and non-numeric columns are carried through untouched. ```python def zscore_numeric(df: pd.DataFrame) -> pd.DataFrame: ... ``` Requirements: - Do not mutate the input. - Use the population standard deviation (`ddof=0`). The pandas default for `Series.std` is `ddof=1`. - Leave non-numeric columns exactly as they are. - Standardized columns must be `float64`. - Preserve the original index and column order. Example: ```python df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]}) out = zscore_numeric(df) # out["a"] -> [-1.224745, 0.0, 1.224745] (ddof=0) # out["b"] -> ["x", "y", "z"] (unchanged) ``` ```hint Picking the columns Choose the columns by their dtype, and work on a copy so the caller's DataFrame never changes. ``` ```hint A pitfall to test Run your function on a column whose values are all equal and look at what comes out. ``` ### Clarifying Questions to Ask - Should boolean columns count as numeric, or pass through unchanged? - How should missing values be treated: skipped when computing the mean and standard deviation, and left missing in the output? - Do nullable extension types such as `Int64` and `Float64` need to be standardized too? ### What a Strong Answer Covers - Selecting the numeric columns by dtype while leaving every other column, the index and the column order untouched - Why the pandas default for `std` gives different numbers, and how to confirm the right `ddof` on the example - Copying the input and producing `float64` output columns - What happens with missing values and with a constant column ### Follow-up Questions - How would you handle a numeric column whose standard deviation is 0? - How would you select the numeric columns reliably across int, float, bool and datetime dtypes? - How would you standardize a new batch of data using the means and standard deviations computed on a training set?

Overview: Write a pandas function that standardizes every numeric column of a DataFrame with the population standard deviation and passes all other columns through unchanged. It tests dtype-based column selection, the pandas ddof default, copying without mutation, and handling of constant columns and missing values.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement zscore_numeric, which returns a new DataFrame where each numeric column is standardized as (col - col.mean()) / col.std(ddof=0) and non-numeric columns are carried through untouched.

def zscore_numeric(df: pd.DataFrame) -> pd.DataFrame:
    ...

Requirements:

  • Do not mutate the input.
  • Use the population standard deviation ( ddof=0 ). The pandas default for Series.std is ddof=1 .
  • Leave non-numeric columns exactly as they are.
  • Standardized columns must be float64 .
  • Preserve the original index and column order.

Example:

df = pd.DataFrame({"a": [1, 2, 3], "b": ["x", "y", "z"]})
out = zscore_numeric(df)
# out["a"] -> [-1.224745, 0.0, 1.224745]  (ddof=0)
# out["b"] -> ["x", "y", "z"]             (unchanged)

Clarifying Questions to Ask Guidance

  • Should boolean columns count as numeric, or pass through unchanged?
  • How should missing values be treated: skipped when computing the mean and standard deviation, and left missing in the output?
  • Do nullable extension types such as Int64 and Float64 need to be standardized too?

What a Strong Answer Covers Guidance

  • Selecting the numeric columns by dtype while leaving every other column, the index and the column order untouched
  • Why the pandas default for std gives different numbers, and how to confirm the right ddof on the example
  • Copying the input and producing float64 output columns
  • What happens with missing values and with a constant column

Follow-up Questions Guidance

  • How would you handle a numeric column whose standard deviation is 0?
  • How would you select the numeric columns reliably across int, float, bool and datetime dtypes?
  • How would you standardize a new batch of data using the means and standard deviations computed on a training set?