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 - Pandas fillna() INPUT DataFrame name quantity price Apple 10 5 Banana null 3 Orange 15 4 Problem: Missing value in quantity Column Types: name: object quantity: int price: int ALGORITHM STEPS 1 Load DataFrame Read products data 2 Select Column Target: 'quantity' 3 Apply fillna(0) Replace null with 0 4 Return DataFrame Modified data def fillna_solution(df): df['quantity'].fillna( 0, inplace=True) return df FINAL RESULT name quantity price Apple 10 5 Banana 0 3 Orange 15 4 OK - Success! null replaced with 0 Transformation: null --> 0 via fillna(0) Key Insight: The fillna() method in Pandas replaces missing/null values with a specified value. Using inplace=True modifies the DataFrame directly. You can also use: df['column'] = df['column'].fillna(0) TutorialsPoint - Fill Missing Data | Pandas fillna() Method
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