Challenge - 5 Problems
Dropna Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of dropna on DataFrame with default settings
What is the output of this code?
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, np.nan, 4],
'B': [np.nan, 2, 3, 4]
})
result = df.dropna()
print(result)Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 2, np.nan, 4], 'B': [np.nan, 2, 3, 4] }) result = df.dropna() print(result)
Attempts:
2 left
💡 Hint
dropna() removes rows with any missing values by default.
✗ Incorrect
By default, dropna() removes rows where any column has NaN. Only rows 1 and 3 have no NaN values.
❓ data_output
intermediate2:00remaining
Number of rows after dropna with subset parameter
Given this DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'X': [1, np.nan, 3, 4],
'Y': [np.nan, 2, 3, np.nan],
'Z': [1, 2, 3, 4]
})
How many rows remain after running:df.dropna(subset=['X', 'Y'])
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'X': [1, np.nan, 3, 4], 'Y': [np.nan, 2, 3, np.nan], 'Z': [1, 2, 3, 4] }) result = df.dropna(subset=['X', 'Y']) print(len(result))
Attempts:
2 left
💡 Hint
Rows with NaN in columns X or Y are dropped.
✗ Incorrect
Rows 0 (Y NaN), 1 (X NaN), and 3 (Y NaN) have NaN in X or Y. Only row 2 has no NaN in X or Y, so 1 row remains.
🔧 Debug
advanced2:00remaining
Identify the error in dropna usage
What error does this code raise?
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, 2, np.nan],
'B': [4, np.nan, 6]
})
result = df.dropna(axis=2)
print(result)Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, 2, np.nan], 'B': [4, np.nan, 6] }) result = df.dropna(axis=2) print(result)
Attempts:
2 left
💡 Hint
DataFrame axes are 0 (rows) and 1 (columns).
✗ Incorrect
axis=2 is invalid for DataFrame. Valid axes are 0 or 1. This causes a ValueError.
❓ visualization
advanced2:00remaining
Visualize effect of dropna with thresh parameter
Given this DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3, 4],
'B': [np.nan, 2, 3, np.nan],
'C': [1, 2, np.nan, 4]
})
Which option shows the correct DataFrame after:df.dropna(thresh=2)
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [1, np.nan, 3, 4], 'B': [np.nan, 2, 3, np.nan], 'C': [1, 2, np.nan, 4] }) result = df.dropna(thresh=2) print(result)
Attempts:
2 left
💡 Hint
thresh=2 means keep rows with at least 2 non-NaN values.
✗ Incorrect
All rows have at least 2 non-NaN values, so none are dropped.
🚀 Application
expert3:00remaining
Effect of dropna with how='all' on DataFrame
Consider this DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'X': [np.nan, np.nan, 3, 4],
'Y': [np.nan, 2, 3, 4],
'Z': [np.nan, np.nan, np.nan, 4]
})
What is the output of:df.dropna(how='all')
Data Analysis Python
import pandas as pd import numpy as np df = pd.DataFrame({ 'X': [np.nan, np.nan, 3, 4], 'Y': [np.nan, 2, 3, 4], 'Z': [np.nan, np.nan, np.nan, 4] }) result = df.dropna(how='all') print(result)
Attempts:
2 left
💡 Hint
how='all' drops rows only if all values are NaN.
✗ Incorrect
Row 0 has all NaN values and is dropped. Other rows have at least one non-NaN value and remain.