Fill Missing Data - Problem
You are working with a DataFrame containing product information from an e-commerce inventory system. The DataFrame has three columns: name (product name), quantity (stock quantity), and price (product price).
Due to data collection issues, some entries in the quantity column are missing (represented as NaN or None values). Your task is to clean the data by filling all missing values in the quantity column with 0.
This is a common data preprocessing step in machine learning and data analysis workflows.
| Column Name | Type |
|---|---|
| name | object |
| quantity | int |
| price | int |
Goal: Return the DataFrame with all missing quantity values replaced with 0.
Input & Output
example_1.py โ Basic Missing Values
$
Input:
products = pd.DataFrame({
'name': ['Apple', 'Banana', 'Orange'],
'quantity': [10, None, 5],
'price': [1, 2, 3]
})
โบ
Output:
name quantity price
0 Apple 10 1
1 Banana 0 2
2 Orange 5 3
๐ก Note:
The missing value (None) in the second row's quantity column is replaced with 0, while other values remain unchanged.
example_2.py โ Multiple Missing Values
$
Input:
products = pd.DataFrame({
'name': ['Apple', 'Banana', 'Orange', 'Mango'],
'quantity': [10, np.nan, 5, None],
'price': [1, 2, 3, 4]
})
โบ
Output:
name quantity price
0 Apple 10 1
1 Banana 0 2
2 Orange 5 3
3 Mango 0 4
๐ก Note:
Both np.nan and None values in the quantity column are replaced with 0. The function handles different types of missing value representations.
example_3.py โ No Missing Values
$
Input:
products = pd.DataFrame({
'name': ['Apple', 'Banana'],
'quantity': [10, 15],
'price': [1, 2]
})
โบ
Output:
name quantity price
0 Apple 10 1
1 Banana 15 2
๐ก Note:
When there are no missing values in the quantity column, the DataFrame remains unchanged. This demonstrates the method works correctly for clean data.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Missing Data
Scan the quantity column to locate NaN/None values
2
Apply fillna() Method
Use pandas vectorized operation to replace all missing values
3
Verify Data Types
Ensure quantity column maintains integer type after filling
4
Return Clean DataFrame
Output DataFrame with consistent, complete data
Key Takeaway
๐ฏ Key Insight: Using pandas' `fillna()` method is the most efficient approach because it performs vectorized operations at the C level, making it significantly faster than manual iteration for data cleaning tasks.
Time & Space Complexity
Time Complexity
O(n)
Linear scan through the column, but highly optimized in C
โ Linear Growth
Space Complexity
O(1)
Can modify in place or create copy efficiently
โ Linear Space
Constraints
-
The DataFrame will always have exactly 3 columns:
name,quantity, andprice - 1 โค number of rows โค 105
-
Missing values in quantity column can be
None,NaN, or other pandas null representations - Only the quantity column should be modified - other columns remain unchanged
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code