Fill Missing Data - Problem

You are given a DataFrame products with the following schema:

Column NameType
nameobject
quantityint
priceint

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
Fill Missing Data: DataFrame TransformationInput DataFramenamequantitypriceApple105Banananull3Orange154Grapenull6fillna(0)Output DataFramenamequantitypriceApple105Banana03Orange154Grape06Missing quantity values replaced with 0Result: Complete DataFrame ready for analysis
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
Asked in
Netflix 25 Spotify 20 Uber 18 Airbnb 15
31.2K Views
High Frequency
~5 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen