0
0
Data Analysis Pythondata~20 mins

Forward fill and backward fill in Data Analysis Python - 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 missing data?
Given the following pandas DataFrame with missing values, what will be the result after applying forward fill (ffill)?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, None, None, 4, None, 6]})
result = df.ffill()
print(result)
A
     A
0  1.0
1  1.0
2  NaN
3  4.0
4  4.0
5  6.0
B
     A
0  1.0
1  NaN
2  NaN
3  4.0
4  NaN
5  6.0
C
     A
0  1.0
1  1.0
2  1.0
3  4.0
4  4.0
5  6.0
D
     A
0  NaN
1  NaN
2  NaN
3  4.0
4  4.0
5  6.0
Attempts:
2 left
💡 Hint
Forward fill replaces missing values with the last known non-missing value above it.
Predict Output
intermediate
2:00remaining
What is the output of backward fill on missing data?
Given the following pandas DataFrame with missing values, what will be the result after applying backward fill (bfill)?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, None, None, 4, None, 6]})
result = df.bfill()
print(result)
A
     A
0  1.0
1  NaN
2  NaN
3  4.0
4  NaN
5  6.0
B
     A
0  1.0
1  4.0
2  4.0
3  4.0
4  6.0
5  6.0
C
     A
0  1.0
1  1.0
2  1.0
3  4.0
4  4.0
5  6.0
D
     A
0  NaN
1  4.0
2  4.0
3  4.0
4  6.0
5  NaN
Attempts:
2 left
💡 Hint
Backward fill replaces missing values with the next known non-missing value below it.
data_output
advanced
2:00remaining
How many missing values remain after forward fill?
Given this DataFrame, how many missing values remain after applying forward fill on column 'B' only?
Data Analysis Python
import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [np.nan, 5, np.nan, 7]})
result = df.copy()
result['B'] = result['B'].ffill()
missing_count = result['B'].isna().sum()
print(missing_count)
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Forward fill cannot fill missing values at the start if no previous value exists.
visualization
advanced
3:00remaining
Which plot shows the effect of backward fill on missing data?
You have a time series with missing values. Which plot correctly shows the data after applying backward fill?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

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_bfill = df.bfill()

plt.figure(figsize=(8,4))
plt.plot(df.index, df['Value'], marker='o', label='Original')
plt.plot(df_bfill.index, df_bfill['Value'], marker='x', label='Backward Fill')
plt.legend()
plt.title('Backward Fill Effect')
plt.xlabel('Date')
plt.ylabel('Value')
plt.grid(True)
plt.tight_layout()
plt.show()
AA line plot where missing points are replaced by the next known value, showing steps upward at missing points.
BA line plot where missing points remain as gaps, no fill applied.
CA line plot where missing points are replaced by the previous known value, showing flat steps backward.
DA scatter plot with random points unrelated to the data.
Attempts:
2 left
💡 Hint
Backward fill uses the next valid value to fill missing points, so the line jumps up at missing points.
🧠 Conceptual
expert
2:30remaining
What is the difference between forward fill and backward fill in time series data?
Choose the best explanation of how forward fill and backward fill handle missing values differently in time series data.
AForward fill replaces missing values with zeros; backward fill replaces missing values with the mean of the column.
BForward fill replaces missing values with the next known value after them; backward fill replaces missing values with the last known value before them.
CBoth forward fill and backward fill replace missing values with the average of previous and next known values.
DForward fill replaces missing values with the last known value before them; backward fill replaces missing values with the next known value after them.
Attempts:
2 left
💡 Hint
Think about the direction in which each fill method propagates values.