Build a percentile summary table for the numeric columns of a pandas DataFrame

Quick Overview

Write a pandas function that returns a table of analyst-chosen percentiles for every numeric column of a DataFrame, with exact row labels, row order and dtypes. It tests DataFrame.quantile, selecting columns by dtype and building output that passes a strict frame-equality check.

Build a percentile summary table for the numeric columns of a pandas DataFrame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `percentile_table(df: pd.DataFrame, percentiles: list[float]) -> pd.DataFrame` returning one row per requested percentile and one column per numeric column of `df`. Values come from `DataFrame.quantile` with the default `interpolation='linear'`. Requirements: - Drop non-numeric columns and keep the numeric columns in their original `df` order. - Rows come in the same order as `percentiles`. - The index holds string labels `f"p{int(round(q * 100)):02d}"` (0.05 gives `'p05'`, 0.99 gives `'p99'`) and is named `'percentile'`. - All values are `float64`. Return the frame; no plotting. Example: ```python df = pd.DataFrame({ "user": ["a", "b", "c", "d", "e"], "sessions": [1, 2, 3, 4, 10], "spend": [0.0, 5.0, 5.0, 20.0, 100.0], }) percentile_table(df, percentiles=[0.5, 0.05, 0.95]) # sessions spend # percentile # p50 3.0 5.0 # p05 1.2 1.0 # p95 8.8 84.0 ``` The string column `user` is dropped, the rows follow the order of `percentiles` (not sorted order), and the integer column `sessions` comes back as `float64`. ```hint Let pandas do the arithmetic Look for the pandas calls that select columns by dtype and compute several quantiles in one pass. Most of the remaining work is matching the index labels, the index name and the dtypes exactly. ``` ### Constraints & Assumptions - A typical input is a frame with three numeric columns and one string column, called with `percentiles=[0.05, 0.25, 0.5, 0.75, 0.95, 0.99]`. - The result is compared with `pandas.testing.assert_frame_equal`, so the index labels, the index name, the column order and the dtypes all have to match exactly. - Each percentile is a fraction between 0 and 1. ### What a Strong Answer Covers - Selecting only the numeric columns while keeping their original order. - Computing every requested percentile with linear interpolation, with rows in the caller's order. - Building the index labels and the index name exactly as specified, and returning `float64` values. - The cost of the computation and how it grows with rows and columns. - Edge cases such as missing values, a frame with no numeric columns, and duplicate or fractional percentiles. ### Follow-up Questions - How would you let the caller choose the `interpolation` method? - How would this scale to a very large DataFrame or to streaming data? - What goes wrong with the label format if a caller asks for 0.999 or for the same percentile twice, and how would you fix it?

Overview: Write a pandas function that returns a table of analyst-chosen percentiles for every numeric column of a DataFrame, with exact row labels, row order and dtypes. It tests DataFrame.quantile, selecting columns by dtype and building output that passes a strict frame-equality check.

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

Write percentile_table(df: pd.DataFrame, percentiles: list[float]) -> pd.DataFrame returning one row per requested percentile and one column per numeric column of df. Values come from DataFrame.quantile with the default interpolation='linear'.

Requirements:

  • Drop non-numeric columns and keep the numeric columns in their original df order.
  • Rows come in the same order as percentiles .
  • The index holds string labels f"p{int(round(q * 100)):02d}" (0.05 gives 'p05' , 0.99 gives 'p99' ) and is named 'percentile' .
  • All values are float64 . Return the frame; no plotting.

Example:

df = pd.DataFrame({
    "user": ["a", "b", "c", "d", "e"],
    "sessions": [1, 2, 3, 4, 10],
    "spend": [0.0, 5.0, 5.0, 20.0, 100.0],
})
percentile_table(df, percentiles=[0.5, 0.05, 0.95])

#             sessions  spend
# percentile
# p50              3.0    5.0
# p05              1.2    1.0
# p95              8.8   84.0

The string column user is dropped, the rows follow the order of percentiles (not sorted order), and the integer column sessions comes back as float64.

Constraints & Assumptions

  • A typical input is a frame with three numeric columns and one string column, called with percentiles=[0.05, 0.25, 0.5, 0.75, 0.95, 0.99] .
  • The result is compared with pandas.testing.assert_frame_equal , so the index labels, the index name, the column order and the dtypes all have to match exactly.
  • Each percentile is a fraction between 0 and 1.

What a Strong Answer Covers Guidance

  • Selecting only the numeric columns while keeping their original order.
  • Computing every requested percentile with linear interpolation, with rows in the caller's order.
  • Building the index labels and the index name exactly as specified, and returning float64 values.
  • The cost of the computation and how it grows with rows and columns.
  • Edge cases such as missing values, a frame with no numeric columns, and duplicate or fractional percentiles.

Follow-up Questions Guidance

  • How would you let the caller choose the interpolation method?
  • How would this scale to a very large DataFrame or to streaming data?
  • What goes wrong with the label format if a caller asks for 0.999 or for the same percentile twice, and how would you fix it?