Detect Numbers Stored as Text in pandas Columns

Quick Overview

Write a pandas function that measures how often the values in each text column parse as numbers and labels the column numeric, mixed or mostly text. It tests null handling with pd.to_numeric coercion, threshold logic, rounding and an exact output schema.

Detect Numbers Stored as Text in pandas Columns

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `numeric_stored_as_text(df: pd.DataFrame) -> pd.DataFrame`. For each object or string column, parse its non-null values with `pd.to_numeric(..., errors='coerce')` and compute `parse_rate = (values that parsed) / (non-null values)`. The report must follow these rules: - Consider only object and string columns. Real numeric columns, such as `int64`, are excluded. Nulls are excluded from both the numerator and the denominator. - Drop columns with `parse_rate == 0.0`. - `verdict` is `'numeric'` if `parse_rate == 1.0`, `'mixed'` if `0.5 <= parse_rate < 1.0`, and `'mostly_text'` if `parse_rate < 0.5`. - The columns are `['column', 'non_null', 'n_parseable', 'parse_rate', 'verdict']` with dtypes `object`, `int64`, `int64`, `float64` (rounded to 3 decimal places) and `object`. - Sort by `parse_rate` descending, then `column` ascending, then `reset_index(drop=True)`. For example: ```python df = pd.DataFrame({ "zip_code": ["10001", "94105", "60601", "73301", "02134", "30301", "98101", "33101", "80202", "19103"], "price": ["12.5", "8", "N/A", "15", "unknown", "9.99", "20", None, "7", "11"], "notes": ["call back", "vip", None, "42", "late", "ok", "n/a", "refund", "ok", "new"], "qty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], }) ``` ```text column non_null n_parseable parse_rate verdict 0 zip_code 10 10 1.000 numeric 1 price 9 7 0.778 mixed 2 notes 9 1 0.111 mostly_text ``` Every ZIP code parses. `price` has 9 non-null values and 7 of them parse (`'N/A'` and `'unknown'` do not), so its rate is 7/9. `notes` has a single stray number in 9 non-null values, and `qty` is a real `int64` column, so it is not reported. ```hint Order of operations Take the denominator before coercion turns bad tokens into NaN, and decide whether the verdict thresholds apply to the raw rate or the rounded one. ``` ### Clarifying Questions to Ask - Should a text column that is entirely null appear in the report, given that its parse rate is 0/0? - Do the verdict thresholds apply to the raw parse rate or to the value rounded to 3 decimal places? ### What a Strong Answer Covers - Selecting only object and string columns and excluding real numeric dtypes. - Excluding nulls from both counts and counting a value as parsed when it is not NaN after coercion. - Classifying on the unrounded rate and rounding only for the output. - Handling all-null columns and the rule that drops a rate of zero. - Returning the exact columns, dtypes and sort order. ### Follow-up Questions - How would you treat thousands separators or currency symbols (for example `"1,200"` or `"$5"`) that `to_numeric` rejects? - How would you extend this to detect date-like or boolean-like text columns? - A column of ZIP codes gets the verdict `'numeric'`. Should you cast it, and how would you catch cases like this?

Overview: Write a pandas function that measures how often the values in each text column parse as numbers and labels the column numeric, mixed or mostly text. It tests null handling with pd.to_numeric coercion, threshold logic, rounding and an exact output schema.

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

Write numeric_stored_as_text(df: pd.DataFrame) -> pd.DataFrame. For each object or string column, parse its non-null values with pd.to_numeric(..., errors='coerce') and compute parse_rate = (values that parsed) / (non-null values).

The report must follow these rules:

  • Consider only object and string columns. Real numeric columns, such as int64 , are excluded. Nulls are excluded from both the numerator and the denominator.
  • Drop columns with parse_rate == 0.0 .
  • verdict is 'numeric' if parse_rate == 1.0 , 'mixed' if 0.5 <= parse_rate < 1.0 , and 'mostly_text' if parse_rate < 0.5 .
  • The columns are ['column', 'non_null', 'n_parseable', 'parse_rate', 'verdict'] with dtypes object , int64 , int64 , float64 (rounded to 3 decimal places) and object .
  • Sort by parse_rate descending, then column ascending, then reset_index(drop=True) .

For example:

df = pd.DataFrame({
    "zip_code": ["10001", "94105", "60601", "73301", "02134",
                 "30301", "98101", "33101", "80202", "19103"],
    "price": ["12.5", "8", "N/A", "15", "unknown",
              "9.99", "20", None, "7", "11"],
    "notes": ["call back", "vip", None, "42", "late",
              "ok", "n/a", "refund", "ok", "new"],
    "qty": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
})
     column  non_null  n_parseable  parse_rate      verdict
0  zip_code        10           10       1.000      numeric
1     price         9            7       0.778        mixed
2     notes         9            1       0.111  mostly_text

Every ZIP code parses. price has 9 non-null values and 7 of them parse ('N/A' and 'unknown' do not), so its rate is 7/9. notes has a single stray number in 9 non-null values, and qty is a real int64 column, so it is not reported.

Clarifying Questions to Ask Guidance

  • Should a text column that is entirely null appear in the report, given that its parse rate is 0/0?
  • Do the verdict thresholds apply to the raw parse rate or to the value rounded to 3 decimal places?

What a Strong Answer Covers Guidance

  • Selecting only object and string columns and excluding real numeric dtypes.
  • Excluding nulls from both counts and counting a value as parsed when it is not NaN after coercion.
  • Classifying on the unrounded rate and rounding only for the output.
  • Handling all-null columns and the rule that drops a rate of zero.
  • Returning the exact columns, dtypes and sort order.

Follow-up Questions Guidance

  • How would you treat thousands separators or currency symbols (for example "1,200" or "$5" ) that to_numeric rejects?
  • How would you extend this to detect date-like or boolean-like text columns?
  • A column of ZIP codes gets the verdict 'numeric' . Should you cast it, and how would you catch cases like this?