C. Fills missing values by propagating last valid observation forward
D. Raises an error due to wrong method
Solution
Step 1: Understand forward fill method
method='ffill' fills missing values by copying the last known non-missing value forward.
Step 2: Apply to DataFrame
For column A: 1 stays, None becomes 1, 3 stays, None becomes 3. For B: None stays None (no previous), 2 stays, None becomes 2, 4 stays.
Final Answer:
Fills missing values by propagating last valid observation forward -> Option C
Quick Check:
ffill copies last valid value forward [OK]
Hint: ffill copies previous value forward to fill NaNs [OK]
Common Mistakes:
Thinking fillna(method='ffill') fills with zero
Confusing ffill with dropping rows
Expecting error from method parameter
4.
What is wrong with the following code if the goal is to remove all rows with missing data from DataFrame df?
df.fillna(drop=True)
medium
A. The code should use df.dropna(inplace=True) to modify in place
B. fillna() does not have a drop parameter; use dropna() instead
C. The parameter drop=True should be dropna=True
D. The code is correct and will remove rows with missing data
Solution
Step 1: Check method and parameters
fillna() is for filling missing values and does not accept a drop parameter.
Step 2: Correct method for removing rows
To remove rows with missing data, use dropna() instead.
Final Answer:
fillna() does not have a drop parameter; use dropna() instead -> Option B
Quick Check:
Use dropna() to remove missing data rows [OK]
Hint: Use dropna() to remove rows, fillna() to replace values [OK]
Common Mistakes:
Using fillna() with invalid parameters
Confusing fillna and dropna functions
Expecting fillna to remove rows
5.
You have a DataFrame df with many missing values scattered randomly. You want to keep as much data as possible but avoid rows with more than 2 missing values. Which pandas method and parameter combination is best?
hard
A. Use df.fillna(method='bfill') to fill missing values backward
B. Use df.fillna(0) to replace all missing values with zero
C. Use df.dropna(axis=1) to drop columns with missing values
D. Use df.dropna(thresh=len(df.columns)-2) to drop rows with more than 2 missing values
Solution
Step 1: Understand the threshold parameter
dropna(thresh=n) keeps rows with at least n non-missing values.
Step 2: Calculate threshold for max 2 missing values
If total columns are len(df.columns), then rows must have at least len(df.columns) - 2 non-missing values to keep.
Step 3: Evaluate other options
Filling with zero or backward fill changes data but does not drop rows. Dropping columns removes data unnecessarily.
Final Answer:
Use df.dropna(thresh=len(df.columns)-2) to drop rows with more than 2 missing values -> Option D
Quick Check:
dropna(thresh) keeps rows with enough non-NaNs [OK]
Hint: Use dropna(thresh) to keep rows with few missing values [OK]
Common Mistakes:
Using fillna instead of dropping rows
Dropping columns instead of rows
Misunderstanding thresh parameter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.