Fill Missing Values Per Column in a pandas DataFrame

Quick Overview

Write a pandas function that fills missing values with a different constant in each of three columns, leaves every other column alone, and returns a new DataFrame. It tests per-column imputation, how dtypes behave after a fill, and cleaning code that does not mutate its input.

Fill Missing Values Per Column in a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `fill_by_column(df)` for a DataFrame with columns `'age'` (numeric), `'city'` (string), and `'score'` (numeric). Fill missing values per column: `age` with `0`, `city` with `'Unknown'`, `score` with `-1`. Requirements: - Leave any other column untouched. - Return a new DataFrame with the same shape, columns, and index. - Do not modify `df` in place. Example: ```python df = pd.DataFrame({ 'age': [25, None, 40], 'city': ['NYC', None, 'LA'], 'score': [None, 8.5, None], }) fill_by_column(df) # age city score # 0 25.0 NYC -1.0 # 1 0.0 Unknown 8.5 # 2 40.0 LA -1.0 ``` `age` and `score` print as floats because the `None` entries make both columns `float64`, and filling the gaps does not change the dtype. ```hint More than a scalar Look at what else `fillna` accepts as its `value` argument, and how pandas matches that value to columns. ``` ### What a Strong Answer Covers - How each column gets its own fill value, ideally in a single operation - What happens to columns that are not named in the fill rules, including their missing values - Why the input DataFrame stays unchanged, and what `inplace=True` would change - The dtypes of the filled columns in the returned DataFrame ### Follow-up Questions - Why pass a dict to `fillna` instead of calling it once per column? - How would you fill the numeric columns with their mean instead of a constant, and what changes when the same transform runs at inference time? - What does your function do if the DataFrame has no `city` column? - Is `-1` a safe fill value for `score`? What would you add so a model or an analyst can still tell which scores were missing?

Overview: Write a pandas function that fills missing values with a different constant in each of three columns, leaves every other column alone, and returns a new DataFrame. It tests per-column imputation, how dtypes behave after a fill, and cleaning code that does not mutate its input.

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

Implement fill_by_column(df) for a DataFrame with columns 'age' (numeric), 'city' (string), and 'score' (numeric). Fill missing values per column: age with 0, city with 'Unknown', score with -1.

Requirements:

  • Leave any other column untouched.
  • Return a new DataFrame with the same shape, columns, and index.
  • Do not modify df in place.

Example:

df = pd.DataFrame({
    'age':   [25, None, 40],
    'city':  ['NYC', None, 'LA'],
    'score': [None, 8.5, None],
})
fill_by_column(df)
#     age     city  score
# 0  25.0      NYC   -1.0
# 1   0.0  Unknown    8.5
# 2  40.0       LA   -1.0

age and score print as floats because the None entries make both columns float64, and filling the gaps does not change the dtype.

What a Strong Answer Covers Guidance

  • How each column gets its own fill value, ideally in a single operation
  • What happens to columns that are not named in the fill rules, including their missing values
  • Why the input DataFrame stays unchanged, and what inplace=True would change
  • The dtypes of the filled columns in the returned DataFrame

Follow-up Questions Guidance

  • Why pass a dict to fillna instead of calling it once per column?
  • How would you fill the numeric columns with their mean instead of a constant, and what changes when the same transform runs at inference time?
  • What does your function do if the DataFrame has no city column?
  • Is -1 a safe fill value for score ? What would you add so a model or an analyst can still tell which scores were missing?