Drop Sparse Pandas Rows Below a Non-Null Count Threshold

Quick Overview

Write a pandas function that keeps only the DataFrame rows with at least a given number of non-missing values, then returns a fresh 0-based index without touching the input. It tests row-wise missing-value counting, the threshold boundary, what pandas treats as missing, and index handling.

Drop Sparse Pandas Rows Below a Non-Null Count Threshold

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `drop_sparse_rows(df, min_non_null)` that returns a new DataFrame keeping only rows with at least `min_non_null` non-missing values, with a fresh 0..n-1 `RangeIndex`. Requirements: - Keep rows with at least `min_non_null` non-missing values and drop the rest. - Preserve the original column order and dtypes. - Reset the index with `reset_index(drop=True)`. - Do not mutate the input: return a new DataFrame. Missing means `NaN`, `None`, `NaT` or `pd.NA`, as recognised by `pandas.isna`. For example: ```python df = pd.DataFrame({ 'a': [1, None, 3], 'b': [None, None, 6], 'c': [7, None, 9], }) drop_sparse_rows(df, min_non_null=2) # a b c # 0 1.0 NaN 7.0 # 2 non-null -> kept # 1 3.0 6.0 9.0 # 3 non-null -> kept # (the middle row had 0 non-null values -> dropped) ``` ```hint Count across each row Reduce each row to the number of values that are present and compare it with `min_non_null`, keeping rows that sit exactly on the threshold. Then check what the surviving row labels look like. ``` ### What a Strong Answer Covers - Counting present values per row, with "at least `min_non_null`" semantics at the boundary. - Which values pandas counts as missing, and which placeholders it treats as present. - Resetting the index without adding an extra column, and leaving the input unchanged. - The edge thresholds: 0 or below, equal to the number of columns, and larger than it. ### Follow-up Questions - How would you drop columns instead of rows by the same threshold? - How would you count only specific columns toward the threshold? - How would you give some columns more weight than others in the count?

Overview: Write a pandas function that keeps only the DataFrame rows with at least a given number of non-missing values, then returns a fresh 0-based index without touching the input. It tests row-wise missing-value counting, the threshold boundary, what pandas treats as missing, and index handling.

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

Implement drop_sparse_rows(df, min_non_null) that returns a new DataFrame keeping only rows with at least min_non_null non-missing values, with a fresh 0..n-1 RangeIndex.

Requirements:

  • Keep rows with at least min_non_null non-missing values and drop the rest.
  • Preserve the original column order and dtypes.
  • Reset the index with reset_index(drop=True) .
  • Do not mutate the input: return a new DataFrame.

Missing means NaN, None, NaT or pd.NA, as recognised by pandas.isna.

For example:

df = pd.DataFrame({
    'a': [1, None, 3],
    'b': [None, None, 6],
    'c': [7, None, 9],
})

drop_sparse_rows(df, min_non_null=2)
#      a    b    c
# 0  1.0  NaN  7.0   # 2 non-null -> kept
# 1  3.0  6.0  9.0   # 3 non-null -> kept
# (the middle row had 0 non-null values -> dropped)

What a Strong Answer Covers Guidance

  • Counting present values per row, with "at least min_non_null " semantics at the boundary.
  • Which values pandas counts as missing, and which placeholders it treats as present.
  • Resetting the index without adding an extra column, and leaving the input unchanged.
  • The edge thresholds: 0 or below, equal to the number of columns, and larger than it.

Follow-up Questions Guidance

  • How would you drop columns instead of rows by the same threshold?
  • How would you count only specific columns toward the threshold?
  • How would you give some columns more weight than others in the count?