After running this, what is the content of column 'B' in df?
medium
A. [2, 2, 4, 4]
B. [None, 2, None, 4]
C. [2, 2, 2, 4]
D. [None, 2, 4, 4]
Solution
Step 1: Understand backward fill with inplace=True
Backward fill copies next valid value upwards, modifying df directly.
Step 2: Apply backward fill to column 'B'
Index 0: None filled with next valid 2 Index 1: 2 (valid) Index 2: None filled with next valid 4 Index 3: 4 (valid)
Final Answer:
[2, 2, 4, 4] -> Option A
Quick Check:
Backward fill copies next valid value up [OK]
Hint: Backward fill copies next valid value upward [OK]
Common Mistakes:
Not using inplace=True and expecting df to change
Confusing backward fill with forward fill
Leaving None values unchanged
5. You have a DataFrame df with missing values scattered in multiple columns. You want to fill missing values by first forward filling, then backward filling any remaining missing values. Which code snippet correctly achieves this?
hard
A. df.fillna(method='ffill', method='bfill')
B. df.fillna(method='bfill').fillna(method='ffill')
C. df.fillna(method='ffill').fillna(method='bfill')
D. df.fillna(method=['ffill', 'bfill'])
Solution
Step 1: Understand chaining fillna methods
First forward fill fills missing values from previous valid data, then backward fill fills remaining missing values from next valid data.
Step 2: Analyze options for correct chaining
df.fillna(method='ffill').fillna(method='bfill') chains fillna with method='ffill' then method='bfill', which is correct. df.fillna(method='bfill').fillna(method='ffill') reverses order, which may fill differently. Options C and D use invalid syntax.
Final Answer:
df.fillna(method='ffill').fillna(method='bfill') -> Option C
Quick Check:
Chain forward then backward fill to cover all missing [OK]
Hint: Chain fillna with ffill then bfill to fill all gaps [OK]
Common Mistakes:
Using invalid syntax with multiple methods in one fillna
Reversing order and missing some fills
Expecting one fillna call to do both fills
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.