0
0
Pandasdata~20 mins

Dropping missing values with dropna() in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dropna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dropna() on rows with any missing values
What is the output DataFrame after running the code below?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, None, 4],
    'B': [None, 2, 3, 4],
    'C': [1, 2, 3, 4]
})
result = df.dropna()
print(result)
A
     A    B  C
1  2.0  2.0  2
3  4.0  4.0  4
B
     A    B  C
0  1.0  NaN  1
1  2.0  2.0  2
2  NaN  3.0  3
3  4.0  4.0  4
C
     A    B  C
0  1.0  NaN  1
2  NaN  3.0  3
D
Empty DataFrame
Columns: [A, B, C]
Index: []
Attempts:
2 left
💡 Hint
dropna() removes rows with any missing values by default.
data_output
intermediate
1:30remaining
Number of rows after dropping columns with missing values
Given the DataFrame below, how many rows remain after dropping columns with any missing values?
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [1, 2, 3, 4],
    'Y': [None, 2, None, 4],
    'Z': [1, 2, 3, 4]
})
result = df.dropna(axis=1)
print(len(result))
A2
B4
C0
D3
Attempts:
2 left
💡 Hint
dropna(axis=1) removes columns with missing values, not rows.
🔧 Debug
advanced
1:30remaining
Error raised by dropna() with invalid parameter
What error does the following code raise?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, None, 3]})
result = df.dropna(axis=2)
print(result)
ANo error, outputs DataFrame with missing values dropped
BTypeError: dropna() got an unexpected keyword argument 'axis'
CValueError: No axis named 2 for object type DataFrame
DAttributeError: 'DataFrame' object has no attribute 'dropna'
Attempts:
2 left
💡 Hint
Valid axis values for DataFrame are 0 or 1.
🚀 Application
advanced
2:00remaining
Using dropna() to keep rows with at least 2 non-missing values
Which code correctly drops rows that have less than 2 non-missing values?
Adf.dropna(thresh=2)
Bdf.dropna(min_count=2)
Cdf.dropna(how='any', thresh=2)
Ddf.dropna(how='all')
Attempts:
2 left
💡 Hint
The thresh parameter sets the minimum number of non-NA values required to keep a row.
🧠 Conceptual
expert
2:30remaining
Effect of dropna() with subset parameter on DataFrame
Given the DataFrame below, what is the output after running df.dropna(subset=['B', 'C'])?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [None, 2, None, 4],
    'C': [1, None, 3, 4]
})
result = df.dropna(subset=['B', 'C'])
print(result)
A
     A    B    C
1  2.0  2.0  NaN
3  4.0  4.0  4.0
0  1.0  NaN  1.0
B
     A    B    C
1  2.0  2.0  NaN
3  4.0  4.0  4.0
C
     A    B    C
1  2.0  2.0  NaN
3  4.0  4.0  4.0
2  3.0  NaN  3.0
D
     A    B    C
3  4  4  4
Attempts:
2 left
💡 Hint
dropna with subset only checks specified columns for missing values.