Fill Missing Values in a Pandas DataFrame With Zero

Quick Overview

Write a pandas function that replaces every missing value in a DataFrame with zero and returns a new frame. It tests how filling affects column dtypes, which values pandas counts as missing, and how to avoid mutating the caller's data.

Fill Missing Values in a Pandas DataFrame With Zero

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def fill_zeros(df: pd.DataFrame) -> pd.DataFrame` that returns a new DataFrame in which every missing value has been replaced by `0`. The function must meet these requirements: - Replace all `NaN` values with `0`. - Preserve the shape, the columns and the index. - Do not modify `df` (no in-place fill). - `float64` columns stay `float64`, so filled values appear as `0.0`. Example: ```python import numpy as np import pandas as pd df = pd.DataFrame({"a": [1.0, np.nan], "b": [np.nan, 4.0]}) fill_zeros(df) # a b # 0 1.0 0.0 # 1 0.0 4.0 ``` ```hint Side effects Check whether the method you call returns a new object or edits the DataFrame it was called on, and what it returns in each case. ``` ### What a Strong Answer Covers - A fill that reaches every column in one step while values that were present stay unchanged. - Proof that the caller's DataFrame still holds its missing values after the call. - The dtype of each column after filling, and why the filled cells print as `0.0`. - Which values pandas counts as missing and which ones it leaves alone. - Column types where `0` is a poor or invalid fill value. ### Follow-up Questions - How would you fill only the numeric columns and leave text columns untouched? - What happens with a categorical column, a datetime column or a nullable `Int64` column? - How would you fill each column with its own median, and what goes wrong if you compute those medians on the full dataset before a train/test split? - When is zero the wrong value to fill with in an analysis?

Overview: Write a pandas function that replaces every missing value in a DataFrame with zero and returns a new frame. It tests how filling affects column dtypes, which values pandas counts as missing, and how to avoid mutating the caller's data.

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

Implement def fill_zeros(df: pd.DataFrame) -> pd.DataFrame that returns a new DataFrame in which every missing value has been replaced by 0.

The function must meet these requirements:

  • Replace all NaN values with 0 .
  • Preserve the shape, the columns and the index.
  • Do not modify df (no in-place fill).
  • float64 columns stay float64 , so filled values appear as 0.0 .

Example:

import numpy as np
import pandas as pd

df = pd.DataFrame({"a": [1.0, np.nan], "b": [np.nan, 4.0]})
fill_zeros(df)
#      a    b
# 0  1.0  0.0
# 1  0.0  4.0

What a Strong Answer Covers Guidance

  • A fill that reaches every column in one step while values that were present stay unchanged.
  • Proof that the caller's DataFrame still holds its missing values after the call.
  • The dtype of each column after filling, and why the filled cells print as 0.0 .
  • Which values pandas counts as missing and which ones it leaves alone.
  • Column types where 0 is a poor or invalid fill value.

Follow-up Questions Guidance

  • How would you fill only the numeric columns and leave text columns untouched?
  • What happens with a categorical column, a datetime column or a nullable Int64 column?
  • How would you fill each column with its own median, and what goes wrong if you compute those medians on the full dataset before a train/test split?
  • When is zero the wrong value to fill with in an analysis?