Challenge - 5 Problems
Fillna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of fillna() with a scalar value?
Given the DataFrame below, what will be the result after filling missing values with 0 using
fillna(0)?Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [4, 5, np.nan]}) result = df.fillna(0) print(result)
Attempts:
2 left
💡 Hint
Remember that fillna replaces all NaN values with the given scalar.
✗ Incorrect
fillna(0) replaces all missing values (NaN) with 0. The resulting DataFrame has no NaNs and numeric columns become floats because of NaN replacement.
❓ data_output
intermediate2:00remaining
How many missing values remain after fillna() with method='ffill'?
Consider the DataFrame below. After applying
fillna(method='ffill'), how many missing values remain?Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'X': [np.nan, 2, np.nan, 4, np.nan]}) filled = df.fillna(method='ffill') missing_count = filled.isna().sum().iloc[0] print(missing_count)
Attempts:
2 left
💡 Hint
Forward fill cannot fill NaN at the start of the column.
✗ Incorrect
Forward fill replaces NaNs with the last valid value above. The first NaN remains because there is no previous value to fill from.
🔧 Debug
advanced2:00remaining
Why does this fillna() call raise an error?
What error will this code raise and why?
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1, np.nan, 3]})
df.fillna(inplace=True, value=[0, 0])Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, np.nan, 3]}) df.fillna(inplace=True, value=[0, 0])
Attempts:
2 left
💡 Hint
Check if the length of the list matches the DataFrame length.
✗ Incorrect
The value parameter expects a scalar or dict matching columns. Passing a list of length 2 for a DataFrame with 3 rows causes a ValueError due to length mismatch.
🚀 Application
advanced2:00remaining
Which fillna() call fills missing values with column means?
You want to fill missing values in each column with that column's mean. Which code does this correctly?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, np.nan, 3], 'B': [4, 5, np.nan]})
Attempts:
2 left
💡 Hint
Use a Series with means for each column as the fill value.
✗ Incorrect
df.mean() returns a Series with mean of each column. Passing it to fillna fills NaNs per column with that mean.
🧠 Conceptual
expert2:00remaining
What is the effect of fillna() with limit parameter?
Given a column with consecutive NaNs, what does
fillna(method='bfill', limit=1) do?Attempts:
2 left
💡 Hint
Limit controls how many consecutive NaNs get filled.
✗ Incorrect
The limit parameter restricts the number of consecutive NaNs filled. With limit=1, only the first NaN in each consecutive group is filled.