Rank numeric DataFrame columns by coefficient of variation

Quick Overview

Write a pandas function that computes the mean, sample standard deviation and coefficient of variation of every numeric column, flags high variability and returns a sorted report. It tests select_dtypes, the ddof default, threshold logic and exact output dtypes.

Rank numeric DataFrame columns by coefficient of variation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `cv_ranking(df: pd.DataFrame, high_cv: float = 1.0) -> pd.DataFrame`. For every numeric column, compute the mean, standard deviation and coefficient of variation, then flag its variability. Requirements: - Compute `std` with `Series.std()` (default `ddof=1`), and `cv = std / mean`. - `flag` is `'High variability'` if `cv >= high_cv`, else `'Stable'`. - The columns are exactly `['column', 'mean', 'std', 'cv', 'flag']`, with one row per numeric column. - Sort by `cv` descending, then `column` ascending, then call `reset_index(drop=True)`. - `column` and `flag` are `object` and the other columns are `float64`. The result is graded with `assert_frame_equal`. ```hint Matching the grader If you reach for numpy to compute the spread, compare its default `ddof` with the one pandas uses. ``` ### Constraints & Assumptions - Every numeric column in the test frame has a strictly positive mean. ### What a Strong Answer Covers - Selecting exactly the numeric columns - The sample standard deviation (`ddof=1`) and `cv` as `std / mean` - An inclusive threshold for the flag - Exact column order, dtypes, sort keys and a reset index - What the coefficient of variation assumes about the data, and when it stops being meaningful ### Follow-up Questions - How would you handle a column whose mean is zero or negative? - Why does the coefficient of variation only make sense for data with a true zero, such as counts or prices, and not for temperatures in Celsius? - A single extreme value makes one column rank first. How would you make the ranking less sensitive to outliers?

Overview: Write a pandas function that computes the mean, sample standard deviation and coefficient of variation of every numeric column, flags high variability and returns a sorted report. It tests select_dtypes, the ddof default, threshold logic and exact output dtypes.

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

Implement cv_ranking(df: pd.DataFrame, high_cv: float = 1.0) -> pd.DataFrame. For every numeric column, compute the mean, standard deviation and coefficient of variation, then flag its variability.

Requirements:

  • Compute std with Series.std() (default ddof=1 ), and cv = std / mean .
  • flag is 'High variability' if cv >= high_cv , else 'Stable' .
  • The columns are exactly ['column', 'mean', 'std', 'cv', 'flag'] , with one row per numeric column.
  • Sort by cv descending, then column ascending, then call reset_index(drop=True) .
  • column and flag are object and the other columns are float64 . The result is graded with assert_frame_equal .

Constraints & Assumptions

  • Every numeric column in the test frame has a strictly positive mean.

What a Strong Answer Covers Guidance

  • Selecting exactly the numeric columns
  • The sample standard deviation ( ddof=1 ) and cv as std / mean
  • An inclusive threshold for the flag
  • Exact column order, dtypes, sort keys and a reset index
  • What the coefficient of variation assumes about the data, and when it stops being meaningful

Follow-up Questions Guidance

  • How would you handle a column whose mean is zero or negative?
  • Why does the coefficient of variation only make sense for data with a true zero, such as counts or prices, and not for temperatures in Celsius?
  • A single extreme value makes one column rank first. How would you make the ranking less sensitive to outliers?