📖 Scenario: Imagine you have a small dataset of daily temperatures, but some days are missing values. You want to fill these missing values to keep your data complete for analysis.
🎯 Goal: You will create a dataset with missing values, set a configuration for filling method, apply forward fill and backward fill to fill missing data, and finally display the filled dataset.
📋 What You'll Learn
Create a pandas DataFrame with specific temperature data including missing values
Create a variable to hold the fill method ('ffill' or 'bfill')
Use the fillna() method with the chosen fill method to fill missing values
Print the resulting DataFrame after filling missing values
💡 Why This Matters
🌍 Real World
Filling missing data is common in real-world datasets like weather records, sales data, or sensor readings to prepare data for analysis.
💼 Career
Data scientists and analysts often need to clean data by filling missing values to build accurate models and reports.
Progress0 / 4 steps
1
Create the temperature DataFrame with missing values
Create a pandas DataFrame called temps with a column 'Temperature' and these exact values: 20.5, NaN, 22.0, NaN, 21.5.
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary. Use np.nan for missing values.
2
Set the fill method configuration
Create a variable called fill_method and set it to the string 'ffill' to indicate forward fill.
Data Analysis Python
Hint
Just assign the string 'ffill' to the variable fill_method.
3
Apply forward fill or backward fill to fill missing values
Create a new DataFrame called filled_temps by applying temps.fillna() with the parameter method=fill_method to fill missing values.
Data Analysis Python
Hint
Use temps.fillna(method=fill_method) to fill missing values.
4
Print the filled DataFrame
Print the DataFrame filled_temps to display the result after filling missing values.
Data Analysis Python
Hint
Use print(filled_temps) to show the DataFrame.
Practice
(1/5)
1. What does forward fill do when handling missing data in a dataset?
easy
A. It removes rows with missing values.
B. It copies the next known value backward to fill missing spots.
C. It replaces missing values with zero.
D. It copies the last known value forward to fill missing spots.
Solution
Step 1: Understand forward fill concept
Forward fill means filling missing data by copying the last known value forward to the next missing spot.
Step 2: Compare options with definition
"It copies the last known value forward to fill missing spots." matches this definition exactly, while others describe different methods.
Final Answer:
It copies the last known value forward to fill missing spots. -> Option D
Quick Check:
Forward fill = copy last known value forward [OK]
Hint: Forward fill copies previous value forward to fill gaps [OK]
Common Mistakes:
Confusing forward fill with backward fill
Thinking forward fill removes missing data
Assuming forward fill replaces with zero
2. Which of the following is the correct syntax to apply forward fill on a pandas DataFrame df?
easy
A. df.fillna(method='ffill')
B. df.fillna(method='bfill')
C. df.fillna(forward=True)
D. df.fillna(direction='forward')
Solution
Step 1: Recall pandas fillna syntax
In pandas, forward fill is done by fillna(method='ffill').
Step 2: Check options for correct syntax
df.fillna(method='ffill') matches the correct method name 'ffill'. df.fillna(method='bfill') performs backward fill. The remaining options use invalid syntax.
Final Answer:
df.fillna(method='ffill') -> Option A
Quick Check:
Forward fill method = 'ffill' [OK]
Hint: Use method='ffill' for forward fill in pandas [OK]
Common Mistakes:
Using 'bfill' instead of 'ffill' for forward fill
Passing invalid parameters like forward=True
Confusing method names in fillna
3. Given the DataFrame df below, what will be the result of df.bfill()?
Backward fill copies the next known value backward to fill missing spots. Here, missing values at index 1 and 2 get filled with 4.0 from index 3. The last missing at index 4 has no next value, so remains NaN.
Step 2: Match output with options
A
0 1.0
1 4.0
2 4.0
3 4.0
4 NaN matches this result exactly. A
0 1.0
1 4.0
2 4.0
3 4.0
4 4.0 incorrectly fills last NaN with 4.0, others do not match.
Final Answer:
A
0 1.0
1 4.0
2 4.0
3 4.0
4 NaN -> Option A
Quick Check:
Backward fill fills missing with next known value [OK]
Hint: Backward fill uses next known value to fill missing [OK]
Common Mistakes:
Assuming last NaN gets filled when no next value exists
Confusing forward and backward fill results
Misreading index positions in output
4. You run the code below but get an error. What is the mistake?
C. The method name 'forward' is invalid; should be 'ffill'.
D. fillna does not accept method parameter.
Solution
Step 1: Check fillna method parameter
The valid method names are 'ffill' for forward fill and 'bfill' for backward fill. 'forward' is not valid.
Step 2: Identify cause of error
Using 'forward' causes a ValueError. DataFrames can have None values, fillna does not require inplace=True, and it accepts the method parameter.
Final Answer:
The method name 'forward' is invalid; should be 'ffill'. -> Option C
Quick Check:
Use 'ffill' for forward fill method [OK]
Hint: Use 'ffill' not 'forward' as method name in fillna [OK]
Common Mistakes:
Using 'forward' instead of 'ffill'
Thinking inplace=True is mandatory
Believing fillna can't handle None values
5. You have a DataFrame df with missing values scattered in column 'B'. You want to fill missing values by first forward filling, then backward filling any remaining missing values. Which code correctly does this?
hard
A. df['B'].fillna(method='ffill', method='bfill')
B. df['B'].fillna(method='ffill').fillna(method='bfill')
C. df['B'].fillna(method='bfill').fillna(method='ffill')
D. df['B'].fillna(method=['ffill', 'bfill'])
Solution
Step 1: Understand chaining fillna calls
To fill missing values first forward then backward, chain fillna calls: first with method='ffill', then on result with method='bfill'.
Step 2: Evaluate options for correct chaining
df['B'].fillna(method='ffill').fillna(method='bfill') correctly chains fillna twice. df['B'].fillna(method='bfill').fillna(method='ffill') reverses order. Attempting multiple methods in one call (duplicate kwargs or list) uses invalid syntax.
Final Answer:
df['B'].fillna(method='ffill').fillna(method='bfill') -> Option B
Quick Check:
Chain fillna with 'ffill' then 'bfill' [OK]
Hint: Chain fillna with 'ffill' then 'bfill' to fill all gaps [OK]
Common Mistakes:
Trying to pass multiple methods in one fillna call
Reversing forward and backward fill order
Not chaining calls, so some NaNs remain
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.