Add a Revenue Column, Filter and Sort a DataFrame in One pandas Method Chain

Quick Overview

Write one pandas method chain that adds a revenue column, keeps rows with revenue of at least 100, sorts them in descending order with stable ties and resets the index. It tests assign with a callable, query, stable sorting and leaving the input DataFrame unchanged.

Add a Revenue Column, Filter and Sort a DataFrame in One pandas Method Chain

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `def enrich_and_filter(df: pd.DataFrame) -> pd.DataFrame` as a single `pandas` method chain starting from `df.assign(...)` (no intermediate variables, no mutation of the input). The input has columns `'price'` (float) and `'qty'` (int). Add `'revenue'`, keep rows with revenue of at least 100, sort descending (stable), and reset the index. The result must meet these requirements: - Add the column with `.assign(revenue=lambda d: d['price'] * d['qty'])`. - Filter with `.query('revenue >= 100')`. - Sort with `.sort_values('revenue', ascending=False, kind='stable')` (ties keep relative order). - Finish with `.reset_index(drop=True)`. - Return columns `'price'`, `'qty'`, `'revenue'`. For example: ```python df = pd.DataFrame({'price': [10.0, 50.0, 3.0], 'qty': [5, 3, 2]}) enrich_and_filter(df) # price qty revenue # 0 50.0 3 150.0 ``` The revenues are 50.0, 150.0 and 6.0, so only the second row reaches the threshold. A second example with ties and a row on the boundary: ```python df = pd.DataFrame({ 'price': [20.0, 40.0, 9.5, 25.0, 100.0], 'qty': [5, 3, 10, 4, 1], }) enrich_and_filter(df) # price qty revenue # 0 40.0 3 120.0 # 1 20.0 5 100.0 # 2 25.0 4 100.0 # 3 100.0 1 100.0 ``` The revenues are 100, 120, 95, 100 and 100. The 95 row is dropped, the three rows at exactly 100 are kept, and they stay in their input order after the 120 row. ```hint Trace the second example Run the second example by hand. Check that your filter keeps a revenue of exactly 100 and that the three tied rows come out in the same order they went in. ``` ### Constraints & Assumptions - `price` is a float column and `qty` is an integer column. - The threshold is inclusive: a revenue of exactly 100 is kept. - The caller's DataFrame must be unchanged after the call. ### What a Strong Answer Covers - One expression from `df.assign(...)` to `.reset_index(drop=True)`, with no intermediate variables. - Why no step of the chain modifies the caller's DataFrame. - An inclusive filter and a stable descending sort, so tied rows keep their input order. - How rows with a missing price or quantity are handled. ### Follow-up Questions - Why prefer `.query()` over boolean masking here? - How would you make the `revenue` threshold a parameter? - How would you inspect the frame between two steps of the chain without splitting it into variables?

Overview: Write one pandas method chain that adds a revenue column, keeps rows with revenue of at least 100, sorts them in descending order with stable ties and resets the index. It tests assign with a callable, query, stable sorting and leaving the input DataFrame unchanged.

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

Implement def enrich_and_filter(df: pd.DataFrame) -> pd.DataFrame as a single pandas method chain starting from df.assign(...) (no intermediate variables, no mutation of the input). The input has columns 'price' (float) and 'qty' (int). Add 'revenue', keep rows with revenue of at least 100, sort descending (stable), and reset the index.

The result must meet these requirements:

  • Add the column with .assign(revenue=lambda d: d['price'] * d['qty']) .
  • Filter with .query('revenue >= 100') .
  • Sort with .sort_values('revenue', ascending=False, kind='stable') (ties keep relative order).
  • Finish with .reset_index(drop=True) .
  • Return columns 'price' , 'qty' , 'revenue' .

For example:

df = pd.DataFrame({'price': [10.0, 50.0, 3.0], 'qty': [5, 3, 2]})
enrich_and_filter(df)
#    price  qty  revenue
# 0   50.0    3    150.0

The revenues are 50.0, 150.0 and 6.0, so only the second row reaches the threshold.

A second example with ties and a row on the boundary:

df = pd.DataFrame({
    'price': [20.0, 40.0, 9.5, 25.0, 100.0],
    'qty':   [5, 3, 10, 4, 1],
})
enrich_and_filter(df)
#    price  qty  revenue
# 0   40.0    3    120.0
# 1   20.0    5    100.0
# 2   25.0    4    100.0
# 3  100.0    1    100.0

The revenues are 100, 120, 95, 100 and 100. The 95 row is dropped, the three rows at exactly 100 are kept, and they stay in their input order after the 120 row.

Constraints & Assumptions

  • price is a float column and qty is an integer column.
  • The threshold is inclusive: a revenue of exactly 100 is kept.
  • The caller's DataFrame must be unchanged after the call.

What a Strong Answer Covers Guidance

  • One expression from df.assign(...) to .reset_index(drop=True) , with no intermediate variables.
  • Why no step of the chain modifies the caller's DataFrame.
  • An inclusive filter and a stable descending sort, so tied rows keep their input order.
  • How rows with a missing price or quantity are handled.

Follow-up Questions Guidance

  • Why prefer .query() over boolean masking here?
  • How would you make the revenue threshold a parameter?
  • How would you inspect the frame between two steps of the chain without splitting it into variables?