3. Given the DataFrame df below, what will df.dropna() return?
import pandas as pd
data = {'A': [1, 2, None], 'B': [4, None, 6]}
df = pd.DataFrame(data)
print(df.dropna())
medium
A. Rows with index 0 only
B. Rows with index 0 and 1
C. Rows with index 1 and 2
D. Empty DataFrame
Solution
Step 1: Examine the DataFrame rows for missing values
Row 0: A=1, B=4 (no missing) Row 1: A=2, B=NaN (missing in B) Row 2: A=NaN, B=6 (missing in A)
Step 2: Apply dropna() default behavior
By default, dropna() drops rows with any missing values, so only row 0 remains.
Final Answer:
Rows with index 0 only -> Option A
Quick Check:
Only complete rows remain [OK]
Hint: dropna() keeps only rows without any NaN [OK]
Common Mistakes:
Assuming it drops columns instead of rows
Thinking it fills missing values
Expecting multiple rows to remain
4. What is wrong with this code snippet if the goal is to drop columns with any missing values?
df.dropna(axis=2)
medium
A. axis=2 is invalid; axis should be 0 or 1
B. dropna cannot drop columns
C. Missing inplace=True to modify df
D. dropna only works on rows by default
Solution
Step 1: Check valid axis values for dropna
Valid axis values are 0 (rows) and 1 (columns). axis=2 is invalid and causes an error.
Step 2: Understand error cause
Using axis=2 will raise a ValueError because pandas does not support axis=2 for DataFrames.
Final Answer:
axis=2 is invalid; axis should be 0 or 1 -> Option A
Quick Check:
axis must be 0 or 1 [OK]
Hint: Only axis=0 or 1 allowed in dropna [OK]
Common Mistakes:
Using axis=2 by mistake
Forgetting axis parameter
Confusing inplace with axis
5. You have a DataFrame df with some missing values. You want to drop only those rows where all columns are missing values. Which dropna() parameter should you use?
hard
A. dropna(axis=1)
B. dropna(how='all')
C. dropna(thresh=2)
D. dropna(how='any')
Solution
Step 1: Understand the how parameter in dropna()
how='all' drops rows only if all values are missing. how='any' drops rows if any value is missing.
Step 2: Identify correct parameter for dropping rows with all missing
Since the goal is to drop rows where all columns are missing, use how='all'.
Final Answer:
dropna(how='all') -> Option B
Quick Check:
how='all' drops rows with all NaN [OK]
Hint: Use how='all' to drop rows with all missing [OK]
Common Mistakes:
Using how='any' which drops too many rows
Confusing thresh with how
Dropping columns instead of rows
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.