Missing Values

~11 mincode completion

Write fill_missing_with_mean(df, column) that replaces the missing values in one column with that column's mean, and returns the DataFrame.

Examples

The gap is filled with the mean of the values that were present

Input
fill_missing_with_mean({"x": [1, None, 3]}, "x")
Output
[1, 2, 3]

A column with no gaps is left exactly as it was

Input
fill_missing_with_mean({"x": [4, 6]}, "x")
Output
[4, 6]

No rows are dropped, the row count is unchanged

Input
fill_missing_with_mean({"x": [1, None, None, 5]}, "x")
Output
4

Hints

Hint 1

A list comprehension expresses this in one line.

Hint 2

A common slip here: dropped rows instead of filling.

Requirements

  • : a DataFrame

  • column: the name of the column to fill

  • Return the DataFrame with that column's gaps filled.

Constraints

  • Standard library only, no imports required

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~11 min

8 employers weight this skill

2 big tech firms, 2 frontier labs, 1 AI product company, 1 defense company, 1 enterprise vendor, 1 quant fund. Top match scores 63.

Python
import pandas as pd

def fill_missing_with_mean(df, column):
    """
    Fill NaNs in one column with that column's mean.

    Args:
        df: a DataFrame
        column: the name of the column to fill

    Returns:
        The DataFrame with that column's gaps filled.
    """
    # YOUR CODE HERE
    pass

def _values(df, column):
    import pandas as pd
    return [float(v) for v in fill_missing_with_mean(pd.DataFrame(df), column)[column]]


def _row_count(df, column):
    import pandas as pd
    return int(len(fill_missing_with_mean(pd.DataFrame(df), column)))

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.