0
0
Pandasdata~20 mins

Forward fill and backward fill in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fill Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of forward fill on this DataFrame?
Given the DataFrame below, what is the result after applying df.ffill()?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, np.nan, 4, np.nan], 'B': [np.nan, 2, np.nan, np.nan, 5]})
result = df.ffill()
A{'A': [1.0, 1.0, 1.0, 4.0, 4.0], 'B': [nan, 2.0, 2.0, 2.0, 5.0]}
B{'A': [1.0, nan, nan, 4.0, nan], 'B': [nan, 2.0, nan, nan, 5.0]}
C{'A': [1.0, 1.0, 1.0, 4.0, nan], 'B': [nan, 2.0, 2.0, 2.0, 5.0]}
D{'A': [1.0, 1.0, 1.0, 4.0, 4.0], 'B': [nan, 2.0, nan, nan, 5.0]}
Attempts:
2 left
💡 Hint
Forward fill replaces NaN with the last known non-NaN value going down the column.
Predict Output
intermediate
2:00remaining
What is the output of backward fill on this DataFrame?
Given the DataFrame below, what is the result after applying df.bfill()?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'X': [np.nan, 3, np.nan, 7, np.nan], 'Y': [1, np.nan, np.nan, 4, 5]})
result = df.bfill()
A{'X': [3.0, 3.0, 7.0, 7.0, 7.0], 'Y': [1.0, 4.0, 4.0, 4.0, 5.0]}
B{'X': [3.0, 3.0, 7.0, 7.0, nan], 'Y': [1.0, 4.0, 4.0, 4.0, 5.0]}
C{'X': [nan, 3.0, 7.0, 7.0, nan], 'Y': [1.0, nan, nan, 4.0, 5.0]}
D{'X': [nan, 3.0, nan, 7.0, nan], 'Y': [1.0, nan, nan, 4.0, 5.0]}
Attempts:
2 left
💡 Hint
Backward fill replaces NaN with the next known non-NaN value going down the column.
data_output
advanced
2:00remaining
How many missing values remain after forward fill with limit?
Given the DataFrame below, after applying df.ffill(limit=1), how many NaN values remain?
Pandas
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [np.nan, np.nan, 2, np.nan, np.nan, 5]})
result = df.ffill(limit=1)
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Limit controls how many consecutive NaNs get filled forward.
visualization
advanced
3:00remaining
Which plot correctly shows the effect of forward fill on missing data?
You have a time series with missing values. Which plot below correctly shows the original data and the data after forward fill?
Pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dates = pd.date_range('2024-01-01', periods=6)
values = [1, np.nan, np.nan, 4, np.nan, 6]
df = pd.DataFrame({'Value': values}, index=dates)
df_ffill = df.ffill()

plt.figure(figsize=(8,4))
plt.plot(df.index, df['Value'], 'o-', label='Original')
plt.plot(df_ffill.index, df_ffill['Value'], 's--', label='Forward Fill')
plt.legend()
plt.title('Forward Fill Effect')
plt.xlabel('Date')
plt.ylabel('Value')
plt.grid(True)
plt.tight_layout()
plt.show()
APlot with original line having gaps at NaNs and forward fill line connecting those gaps with previous values
BPlot with original line continuous and forward fill line with gaps at NaNs
CPlot with both lines continuous and identical
DPlot with original line flat and forward fill line increasing linearly
Attempts:
2 left
💡 Hint
Forward fill fills gaps by extending last known value, so forward fill line has no gaps.
🧠 Conceptual
expert
3:00remaining
What happens if you apply forward fill then backward fill on a DataFrame with missing values?
Consider a DataFrame with missing values scattered. If you apply df.ffill() followed by df.bfill(), what is the resulting effect on missing data?
AAll missing values are filled except those at the start and end of the DataFrame
BOnly missing values between two valid values are filled; missing values at edges remain
CAll missing values are filled because forward fill fills down and backward fill fills up
DNo missing values are filled because the two methods cancel each other out
Attempts:
2 left
💡 Hint
Forward fill fills down, backward fill fills up, so combined they fill all gaps.