Bird
Raised Fist0
Pandasdata~5 mins

Forward fill and backward fill in Pandas - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is forward fill in pandas?
Forward fill is a method to fill missing values by copying the last known non-missing value forward until a new non-missing value appears.
Click to reveal answer
beginner
What does backward fill do in pandas?
Backward fill fills missing values by copying the next known non-missing value backward until a previous non-missing value appears.
Click to reveal answer
beginner
How do you apply forward fill on a pandas DataFrame column named 'A'?
Use df['A'].fillna(method='ffill') to fill missing values in column 'A' by forward filling.
Click to reveal answer
beginner
What is the difference between forward fill and backward fill?
Forward fill uses previous values to fill missing data, while backward fill uses future values to fill missing data.
Click to reveal answer
intermediate
Can forward fill and backward fill be used together?
Yes, you can first apply forward fill to fill missing values, then apply backward fill to fill any remaining missing values.
Click to reveal answer
Which pandas method fills missing values by copying the previous non-missing value?
Afillna(method='ffill')
Bfillna(method='bfill')
Cdropna()
Dinterpolate()
What does fillna(method='bfill') do?
AFills missing values with the previous value
BFills missing values with the next value
CDrops missing values
DReplaces missing values with zero
If a DataFrame has missing values at the start, which fill method will fill them?
ANeither
BForward fill
CBackward fill
DBoth
Which method can you use to fill missing values first forward then backward?
Afillna(method='ffill').fillna(method='bfill')
Bfillna(method='bfill').fillna(method='ffill')
Cdropna()
Dinterpolate()
What happens if you apply forward fill on a column with no previous non-missing values?
AThey get filled with zeros
BAn error occurs
CThey get filled with the next non-missing value
DMissing values remain unchanged
Explain how forward fill and backward fill work to handle missing data in pandas.
Think about filling missing spots by looking backward or forward in the data.
You got /4 concepts.
    Describe a situation where you would use both forward fill and backward fill together.
    Consider missing data at the beginning and middle of a dataset.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the forward fill method do in pandas when handling missing data?
      easy
      A. It copies the next known value backward to fill missing spots.
      B. It copies the last known value forward to fill missing spots.
      C. It replaces missing values with zero.
      D. It removes rows with missing values.

      Solution

      1. Step 1: Understand forward fill concept

        Forward fill means filling missing data by copying the last valid value forward.
      2. Step 2: Compare options with definition

        It copies the last known value forward to fill missing spots. matches this exactly, while others describe different methods.
      3. Final Answer:

        It copies the last known value forward to fill missing spots. -> Option B
      4. Quick Check:

        Forward fill = copy last known value forward [OK]
      Hint: Forward fill copies previous value downwards [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 pandas syntax to perform a backward fill on a DataFrame df?
      easy
      A. df.fillna(method='backfill')
      B. df.fillna(method='ffill')
      C. df.fillna(backward=True)
      D. df.fillna(direction='backward')

      Solution

      1. Step 1: Recall pandas fillna syntax

        Backward fill uses method='backfill' or method='bfill' in fillna.
      2. Step 2: Check options for correct syntax

        df.fillna(method='backfill') uses method='backfill', which is correct. Others are invalid or incorrect syntax.
      3. Final Answer:

        df.fillna(method='backfill') -> Option A
      4. Quick Check:

        Backward fill syntax = method='backfill' [OK]
      Hint: Use method='backfill' for backward fill in fillna [OK]
      Common Mistakes:
      • Using method='ffill' for backward fill
      • Passing invalid parameters like backward=True
      • Confusing method names in fillna
      3. Given the DataFrame:
      import pandas as pd
      df = pd.DataFrame({'A': [1, None, None, 4, None]})
      df_filled = df.fillna(method='ffill')

      What is the content of df_filled?
      medium
      A. [1, None, None, 4, 1]
      B. [1, 1, 1, 4, None]
      C. [1, None, None, 4, None]
      D. [1, 1, 1, 4, 4]

      Solution

      1. Step 1: Understand forward fill on the DataFrame

        Forward fill copies last valid value downwards to fill missing values.
      2. Step 2: Apply forward fill step-by-step

        Index 0: 1 (valid)
        Index 1: filled with 1
        Index 2: filled with 1
        Index 3: 4 (valid)
        Index 4: filled with 4
      3. Final Answer:

        [1, 1, 1, 4, 4] -> Option D
      4. Quick Check:

        Forward fill propagates last valid value forward [OK]
      Hint: Forward fill fills missing with last valid above [OK]
      Common Mistakes:
      • Thinking forward fill leaves trailing NAs unfilled
      • Failing to propagate through consecutive NAs
      • Confusing forward fill with backward fill
      4. Consider this code snippet:
      import pandas as pd
      df = pd.DataFrame({'B': [None, 2, None, 4]})
      df.fillna(method='bfill', inplace=True)

      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

      1. Step 1: Understand backward fill with inplace=True

        Backward fill copies next valid value upwards, modifying df directly.
      2. 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)
      3. Final Answer:

        [2, 2, 4, 4] -> Option A
      4. 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

      1. 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.
      2. 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.
      3. Final Answer:

        df.fillna(method='ffill').fillna(method='bfill') -> Option C
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard