0
0
Data Analysis Pythondata~20 mins

Filling missing values (fillna) in Data Analysis Python - 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
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)
A
     A
0  1.0
1  NaN
2  NaN
3  4.0
4  5.0
B
     A
0  1.0
1  1.0
2  1.0
3  4.0
4  5.0
C
     A
0  1.0
1  2.0
2  3.0
3  4.0
4  5.0
D
     A
0  NaN
1  NaN
2  NaN
3  4.0
4  5.0
Attempts:
2 left
💡 Hint
Forward fill replaces missing values with the last known non-missing value above.
data_output
intermediate
1: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)
A3
B1
C2
D0
Attempts:
2 left
💡 Hint
Filling missing values with 0 replaces all NaNs.
🔧 Debug
advanced
2: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)
ANo error, DataFrame is updated in place with NaNs replaced by 5
BTypeError: fillna() missing 1 required positional argument: 'value'
CTypeError: fillna() got multiple values for argument 'value'
DAttributeError: 'DataFrame' object has no attribute 'fillna'
Attempts:
2 left
💡 Hint
Check the order and naming of arguments in fillna method.
🚀 Application
advanced
2: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]})
Adf['D'].fillna(df['D'].median(), inplace=True)
Bdf.fillna(df.mean(), inplace=True)
Cdf['D'] = df['D'].fillna(df['D'].mean())
Ddf.fillna(df['D'].mean())
Attempts:
2 left
💡 Hint
You want to fill only column 'D' with its mean, not the whole DataFrame.
🧠 Conceptual
expert
3: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'?
A
     E
0  NaN
1  3.0
2  3.0
3  5.0
4  5.0
B
     E
0  3.0
1  3.0
2  3.0
3  5.0
4  5.0
C
     E
0  3.0
1  3.0
2  3.0
3  NaN
4  5.0
D
     E
0  3.0
1  NaN
2  3.0
3  5.0
4  5.0
Attempts:
2 left
💡 Hint
The limit parameter restricts how many consecutive NaNs are filled in one go.