Challenge - 5 Problems
Fill Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Forward fill replaces NaN with the last known non-NaN value going down the column.
✗ Incorrect
Forward fill fills missing values by propagating the last valid observation forward. So NaNs after a number get replaced by that number until a new number appears.
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Backward fill replaces NaN with the next known non-NaN value going down the column.
✗ Incorrect
Backward fill fills missing values by propagating the next valid observation backward. So NaNs before a number get replaced by that number until a new number appears.
❓ data_output
advanced2: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)
Attempts:
2 left
💡 Hint
Limit controls how many consecutive NaNs get filled forward.
✗ Incorrect
With limit=1, only one NaN after a valid value is filled. The first two NaNs remain because no previous value exists. The NaN after 2 is filled once, but the next NaN remains.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Forward fill fills gaps by extending last known value, so forward fill line has no gaps.
✗ Incorrect
Original data has gaps where NaNs are. Forward fill replaces NaNs with last known value, so line is continuous without gaps at those points.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Forward fill fills down, backward fill fills up, so combined they fill all gaps.
✗ Incorrect
Forward fill fills missing values by propagating last valid value downward. Backward fill fills missing values by propagating next valid value upward. Together, they fill all missing values except if entire column is NaN.