Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Dropping missing values (dropna) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 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 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)
A
     A    B
0  1.0  NaN
1  2.0  2.0
2  NaN  3.0
3  4.0  4.0
B
     A    B
0  1.0  NaN
1  2.0  2.0
C
     A    B
1  2.0  2.0
3  4.0  4.0
D
Empty DataFrame
Columns: [A, B]
Index: []
Attempts:
2 left
💡 Hint
dropna() removes rows with any missing values by default.
❓ data_output
intermediate
2: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))
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Rows with NaN in columns X or Y are dropped.
🔧 Debug
advanced
2: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)
ANo error, prints DataFrame with dropped columns
BTypeError: dropna() got an unexpected keyword argument 'axis'
CAttributeError: 'DataFrame' object has no attribute 'dropna'
DValueError: No axis named 2 for object type DataFrame
Attempts:
2 left
💡 Hint
DataFrame axes are 0 (rows) and 1 (columns).
❓ visualization
advanced
2: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)
A
     A    B    C
1  NaN  2.0  2.0
2  3.0  3.0  NaN
B
     A    B    C
0  1.0  NaN  1.0
1  NaN  2.0  2.0
2  3.0  3.0  NaN
3  4.0  NaN  4.0
C
     A    B    C
0  1.0  NaN  1.0
1  NaN  2.0  2.0
2  3.0  3.0  NaN
D
Empty DataFrame
Columns: [A, B, C]
Index: []
Attempts:
2 left
💡 Hint
thresh=2 means keep rows with at least 2 non-NaN values.
🚀 Application
expert
3: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)
A
     X    Y    Z
1  NaN  2.0  NaN
2  3.0  3.0  NaN
3  4.0  4.0  4.0
B
     X    Y    Z
2  3.0  3.0  NaN
3  4.0  4.0  4.0
C
     X    Y    Z
0  NaN  NaN  NaN
1  NaN  2.0  NaN
2  3.0  3.0  NaN
3  4.0  4.0  4.0
D
Empty DataFrame
Columns: [X, Y, Z]
Index: []
Attempts:
2 left
💡 Hint
how='all' drops rows only if all values are NaN.

Practice

(1/5)
1. What does the dropna() function do in a pandas DataFrame?
easy
A. Duplicates rows with missing values
B. Fills missing values with zeros
C. Sorts the DataFrame by index
D. Removes rows or columns with missing values

Solution

  1. Step 1: Understand the purpose of dropna()

    The dropna() function is used to remove missing data (NaN values) from a DataFrame.
  2. Step 2: Identify what is removed

    It removes rows or columns that contain missing values, depending on parameters.
  3. Final Answer:

    Removes rows or columns with missing values -> Option D
  4. Quick Check:

    dropna() removes missing data [OK]
Hint: Remember: dropna means drop missing data [OK]
Common Mistakes:
  • Confusing dropna with fillna
  • Thinking it sorts data
  • Assuming it duplicates data
2. Which of the following is the correct syntax to drop rows with any missing values in a DataFrame df?
easy
A. df.dropna(inplace=False, axis=2)
B. df.dropna(axis=1)
C. df.dropna(axis=0)
D. df.dropna(axis='columns')

Solution

  1. Step 1: Understand axis parameter in dropna()

    axis=0 means drop rows, axis=1 means drop columns.
  2. Step 2: Identify correct axis for dropping rows

    To drop rows with missing values, use axis=0.
  3. Final Answer:

    df.dropna(axis=0) -> Option C
  4. Quick Check:

    axis=0 drops rows [OK]
Hint: axis=0 drops rows, axis=1 drops columns [OK]
Common Mistakes:
  • Using axis=1 to drop rows
  • Using invalid axis=2
  • Confusing inplace parameter
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

  1. 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)
  2. Step 2: Apply dropna() default behavior

    By default, dropna() drops rows with any missing values, so only row 0 remains.
  3. Final Answer:

    Rows with index 0 only -> Option A
  4. 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

  1. 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.
  2. Step 2: Understand error cause

    Using axis=2 will raise a ValueError because pandas does not support axis=2 for DataFrames.
  3. Final Answer:

    axis=2 is invalid; axis should be 0 or 1 -> Option A
  4. 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

  1. 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.
  2. 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'.
  3. Final Answer:

    dropna(how='all') -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard