Compare a feature's mean across the two classes of a binary target in pandas

Quick Overview

Write a pandas function that compares the mean of a numeric feature between the two classes of a binary target and returns both means and their absolute gap as a float64 Series. It tests boolean masking, matching an exact output contract for a strict Series comparison, and basic exploratory analysis of class differences.

Compare a feature's mean across the two classes of a binary target in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `feature_means_by_target(df, feature, target)`, where `target` names an integer 0/1 column and `feature` a numeric column. Return a `float64` `pd.Series` with index exactly `['mean_target_0', 'mean_target_1', 'abs_gap']` and `name=None`. - `mean_target_0` / `mean_target_1`: mean of `feature` where `target == 0` / `target == 1`. - `abs_gap` = `abs(mean_target_1 - mean_target_0)`. The output is graded with `assert_series_equal`, so values, dtype, index order and name must all match. Example: ```python # df has feature=[1.0, 2.0, 5.0, 7.0], target=[0, 0, 1, 1] feature_means_by_target(df, 'feature', 'target') # mean_target_0 1.5 # mean_target_1 6.0 # abs_gap 4.5 # dtype: float64 ``` ```hint Watch the Series name A strict comparison checks the name as well as the values. Look at what name a Series inherits when it comes from a column selection or a `groupby` result. ``` ### Constraints & Assumptions - Both classes are present in `target`. - `feature` is numeric with no missing values, so no missing-value handling is needed. ### What a Strong Answer Covers - Computing each class mean only from the rows where the target equals that class. - The absolute gap between the two means. - Building a `float64` Series with the exact index order and no name. - What would break if a class were missing or the feature had `NaN`, even though the task rules both out. ### Follow-up Questions - How would you handle a missing class or `NaN` values in the feature? - A raw mean gap depends on the feature's units. How would you make gaps comparable across features? - When can a large mean gap mislead you about how useful a feature is for predicting the target? - How would you compute the same summary for every numeric feature at once?

Overview: Write a pandas function that compares the mean of a numeric feature between the two classes of a binary target and returns both means and their absolute gap as a float64 Series. It tests boolean masking, matching an exact output contract for a strict Series comparison, and basic exploratory analysis of class differences.

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

Implement feature_means_by_target(df, feature, target), where target names an integer 0/1 column and feature a numeric column. Return a float64 pd.Series with index exactly ['mean_target_0', 'mean_target_1', 'abs_gap'] and name=None.

  • mean_target_0 / mean_target_1 : mean of feature where target == 0 / target == 1 .
  • abs_gap = abs(mean_target_1 - mean_target_0) .

The output is graded with assert_series_equal, so values, dtype, index order and name must all match.

Example:

# df has feature=[1.0, 2.0, 5.0, 7.0], target=[0, 0, 1, 1]

feature_means_by_target(df, 'feature', 'target')
# mean_target_0    1.5
# mean_target_1    6.0
# abs_gap          4.5
# dtype: float64

Constraints & Assumptions

  • Both classes are present in target .
  • feature is numeric with no missing values, so no missing-value handling is needed.

What a Strong Answer Covers Guidance

  • Computing each class mean only from the rows where the target equals that class.
  • The absolute gap between the two means.
  • Building a float64 Series with the exact index order and no name.
  • What would break if a class were missing or the feature had NaN , even though the task rules both out.

Follow-up Questions Guidance

  • How would you handle a missing class or NaN values in the feature?
  • A raw mean gap depends on the feature's units. How would you make gaps comparable across features?
  • When can a large mean gap mislead you about how useful a feature is for predicting the target?
  • How would you compute the same summary for every numeric feature at once?