Challenge - 5 Problems
Fillna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of fillna with forward fill method
What is the output of the following code snippet?
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, np.nan, np.nan, 4, 5]}) result = df.fillna(method='ffill') print(result)
Attempts:
2 left
💡 Hint
Forward fill replaces missing values with the last known non-missing value above.
✗ Incorrect
The fillna method with method='ffill' fills missing values by propagating the last valid observation forward. So the NaNs at index 1 and 2 become 1.0.
❓ data_output
intermediate1:30remaining
Number of missing values after fillna with a constant
After running the code below, how many missing values remain in the DataFrame?
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({'B': [np.nan, 2, np.nan, 4, 5]}) df_filled = df.fillna(0) missing_count = df_filled.isna().sum().sum() print(missing_count)
Attempts:
2 left
💡 Hint
Filling missing values with 0 replaces all NaNs.
✗ Incorrect
fillna(0) replaces all NaN values with 0, so no missing values remain.
🔧 Debug
advanced2:00remaining
Identify the error in fillna usage
What error will this code raise?
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({'C': [1, np.nan, 3]}) df.fillna(inplace=True, value=5)
Attempts:
2 left
💡 Hint
Check the order and naming of arguments in fillna method.
✗ Incorrect
The fillna method expects the first argument to be the value to fill. Using both positional and keyword arguments for 'value' causes a TypeError.
🚀 Application
advanced2:30remaining
Fill missing values with column mean
Given the DataFrame below, which code correctly fills missing values in column 'D' with the mean of that column?
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({'D': [1, np.nan, 3, np.nan, 5]})
Attempts:
2 left
💡 Hint
You want to fill only column 'D' with its mean, not the whole DataFrame.
✗ Incorrect
Option C fills only column 'D' with its mean and assigns back to the column. Option C fills all columns with their means, which is not asked. Option C uses median, not mean. Option C does not assign back or use inplace, so no change.
🧠 Conceptual
expert3:00remaining
Effect of fillna with limit parameter
Consider this DataFrame and code:
import pandas as pd
import numpy as np
df = pd.DataFrame({'E': [np.nan, np.nan, 3, np.nan, 5]})
result = df.fillna(method='bfill', limit=1)
What is the content of 'result'?
Attempts:
2 left
💡 Hint
The limit parameter restricts how many consecutive NaNs are filled in one go.
✗ Incorrect
The fillna method with method='bfill' fills missing values by propagating the next valid observation backward. With limit=1, it fills only one consecutive NaN preceding each valid value. So the NaN at index 1 becomes 3.0, index 0 remains NaN, and index 3 becomes 5.0.