9. Replace Missing Values
Difficulty: Easy · Topics: Null Handling
The products table has gaps. Return all columns with:
- missing
categoryreplaced by"Unknown" - missing
pricereplaced by0
An empty string "" is not a missing value.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
products
| id | name | category | price |
|---|---|---|---|
| 1 | Pen | Stationery | 1.5 |
| 2 | Notebook | null | 3 |
| 3 | Mug | Kitchen | null |
| 4 | Lamp | null | null |
Expected output
| id | name | category | price |
|---|---|---|---|
| 1 | Pen | Stationery | 1.5 |
| 2 | Notebook | Unknown | 3 |
| 3 | Mug | Kitchen | 0 |
| 4 | Lamp | Unknown | 0 |
Hints
Hint 1
df.fillna({"col": value}) accepts a dict of per-column defaults.Hint 2
Alternatively useF.coalesce(col, F.lit(default)) inside withColumn.Learn the concepts
- fillna / dropna · 4 min read. Fill or drop missing values per column, and why fillna(0) leaves string columns alone.
PySpark functions you'll practise
- fillna
Related problems
- Data Quality: Null Count per Column · Medium · Null Handling, Filtering & Selection
- Label Late Shipments · Easy · Dates, Null Handling, Conditional Logic
- Joining on Nullable Keys · Medium · Joins, Null Handling, Filtering & Selection
- Unpivot Wide Columns to Rows · Medium · Pivot, Unpivot & Rollup, Null Handling, Filtering & Selection
- Filter Low-Quality Training Text · Medium · Data Prep for AI, Strings, Null Handling
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice