Filter Numeric Values Stored as Strings
PYTHON coding challenge · Difficulty: easy · Topic: Filtering · +70 XP
DataFrame: transactions
+----------+--------+
| Column | Type |
+----------+--------+
| tx_id | int | | customer | object | | amount | object |
+----------+--------+
amount is stored as text, not as a number. Some of those strings are valid
numbers and some are not.
Problem
-------
Keep only the rows whose amount is a valid number, and drop the rest.
Counts as VALID
"100" an integer
"250.50" a decimal
"-75" a negative number
" 500 " a number with surrounding whitespace
Counts as INVALID
"abc" text
"N/A" text
"unknown" text
"" an empty string
"1,000" a thousands separator is not a number to pandas
"NaN" parses to a NaN, which is not a value
None missing
Requirements
------------
1. Build a DataFrame called df_result and print it with print(df_result).
2. Keep the same three columns: tx_id, customer, amount.
3. In the output, amount must be the converted NUMBER, not the original string.
" 500 " becomes 500.0.
4. Reset the index with reset_index(drop=True), so the surviving rows are
numbered from 0.
Example Input
-------------
+-------+----------+-----------+
| tx_id | customer | amount |
+-------+----------+-----------+
| 1 | Alice | "100" | | 2 | Bob | "abc" | | 3 | Carol | "250.50" | | 4 | Dan | None | | 5 | Eve | " 500 " | | 6 | Frank | "N/A" | | 7 | Grace | "-75" | | 8 | Henry | "unknown" |
+-------+----------+-----------+
Expected Output
---------------
tx_id customer amount
0 1 Alice 100.0
1 3 Carol 250.5
2 5 Eve 500.0
3 7 Grace -75.0
Explanation
-----------
Rows 1, 3, 5 and 7 hold numbers, so they survive:
"100" -> 100.0
"250.50" -> 250.5
" 500 " -> 500.0 the surrounding spaces are ignored
"-75" -> -75.0
Rows 2, 4, 6 and 8 hold "abc", None, "N/A" and "unknown". None of those is a
number, so they are dropped.
The index restarts at 0 because the surviving rows were re-numbered. Without
reset_index(drop=True) the output would show 0, 2, 4, 6 instead.
If NO row is valid the answer is an empty DataFrame, which pandas prints as:
Empty DataFrame
Columns: [tx_id, customer, amount]
Index: []
What this PYTHON challenge teaches you
“Filter Numeric Values Stored as Strings” is a easy-level PYTHON challenge focused on Filtering. Working through it gives you hands-on practice with pandas, to_numeric, filtering, data cleaning, NaN — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- pandas
- to_numeric
- filtering
- data cleaning
- NaN
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- pd.to_numeric(series, errors="coerce") converts what it can and turns the rest into NaN instead of raising.
- notna() on the converted series is your mask. Remember to put the converted numbers back into amount and to reset_index(drop=True).
How to practise it on PySpark.in
Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYTHON challenges
- Find All Products That Are Out of Stock
- Filter Numeric Values Stored as Strings (SQL)
- Word Frequency Counter
- Validate Unique User IDs
- Extract Error Codes from Log Strings
- Sum of Values by Key
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Filtering.