Fill Missing Data - Problem
You are given a DataFrame products with the following schema:
| Column Name | Type |
|---|---|
| name | object |
| quantity | int |
| price | int |
Write a solution to fill in the missing values as 0 in the quantity column.
Return the modified DataFrame with all missing quantity values replaced with 0.
Input & Output
Example 1 — Basic Missing Values
$
Input:
products = [{'name': 'Apple', 'quantity': 10, 'price': 5}, {'name': 'Banana', 'quantity': null, 'price': 3}, {'name': 'Orange', 'quantity': 15, 'price': 4}]
›
Output:
[{'name': 'Apple', 'quantity': 10, 'price': 5}, {'name': 'Banana', 'quantity': 0, 'price': 3}, {'name': 'Orange', 'quantity': 15, 'price': 4}]
💡 Note:
The Banana row has a missing quantity value (null), which gets replaced with 0. Other rows remain unchanged.
Example 2 — Multiple Missing Values
$
Input:
products = [{'name': 'Laptop', 'quantity': null, 'price': 1000}, {'name': 'Mouse', 'quantity': 25, 'price': 20}, {'name': 'Keyboard', 'quantity': null, 'price': 50}]
›
Output:
[{'name': 'Laptop', 'quantity': 0, 'price': 1000}, {'name': 'Mouse', 'quantity': 25, 'price': 20}, {'name': 'Keyboard', 'quantity': 0, 'price': 50}]
💡 Note:
Both Laptop and Keyboard have missing quantities, both get filled with 0. Mouse quantity remains unchanged.
Example 3 — No Missing Values
$
Input:
products = [{'name': 'Phone', 'quantity': 100, 'price': 500}, {'name': 'Tablet', 'quantity': 50, 'price': 300}]
›
Output:
[{'name': 'Phone', 'quantity': 100, 'price': 500}, {'name': 'Tablet', 'quantity': 50, 'price': 300}]
💡 Note:
No missing values in quantity column, so DataFrame remains exactly the same.
Constraints
- 1 ≤ products.length ≤ 1000
- name is a non-empty string
- quantity can be null or a non-negative integer
- price is a positive integer
Visualization
Tap to expand
Understanding the Visualization
1
Input
DataFrame with missing quantity values (null/NaN)
2
Process
Apply fillna(0) to replace missing values
3
Output
Complete DataFrame with 0 for missing quantities
Key Takeaway
🎯 Key Insight: Use pandas fillna() for efficient vectorized missing value replacement - it's faster and cleaner than manual iteration
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code