0
0
Pandasdata~20 mins

Filling missing values with fillna() in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fillna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A{'A': [1.0, 0.0, 3.0], 'B': [4.0, 5.0, 0.0]}
B{'A': [1, nan, 3], 'B': [4, 5, nan]}
C{'A': [1.0, nan, 3.0], 'B': [4.0, 5.0, nan]}
D{'A': [1, 0, 3], 'B': [4, 5, 0]}
Attempts:
2 left
💡 Hint
Remember that fillna replaces all NaN values with the given scalar.
data_output
intermediate
2: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)
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Forward fill cannot fill NaN at the start of the column.
🔧 Debug
advanced
2: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])
AValueError: Length of values does not match length of index
BTypeError: fillna() got multiple values for argument 'value'
CAttributeError: 'NoneType' object has no attribute 'fillna'
DNo error, fills NaN with 0
Attempts:
2 left
💡 Hint
Check if the length of the list matches the DataFrame length.
🚀 Application
advanced
2: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]})
Adf.fillna(df.median())
Bdf.fillna(df.mean())
Cdf.fillna({'A': df['A'].mean(), 'B': df['B'].median()})
Ddf.fillna(0)
Attempts:
2 left
💡 Hint
Use a Series with means for each column as the fill value.
🧠 Conceptual
expert
2:00remaining
What is the effect of fillna() with limit parameter?
Given a column with consecutive NaNs, what does fillna(method='bfill', limit=1) do?
AFills NaNs only if they are at the start of the column
BFills all NaNs in the column with the next valid value
CFills only the first NaN in each consecutive group, leaving others as NaN
DRaises an error because limit cannot be used with method
Attempts:
2 left
💡 Hint
Limit controls how many consecutive NaNs get filled.