Difficulty: Easy | Acceptance: 64.50% | Paid: No Topics: N/A
Problem Description
Write a solution to drop rows from a DataFrame that contain any missing values.
Given a DataFrame students, drop all rows where any column has a missing value (NaN). Return the resulting DataFrame.
Table of Contents
- Examples
- Constraints
- Approach 1: Using dropna()
- Approach 2: Using Boolean Indexing
- Approach 3: Using notnull()
Examples
Example 1
Input:
+------------+---------+-----+
| student_id | name | age |
+------------+---------+-----+
| 32 | Piper | 5 |
| 217 | None | 19 |
| 779 | Georgia | 20 |
| 849 | Willow | 14 |
+------------+---------+-----+
Output:
+------------+---------+-----+
| student_id | name | age |
+------------+---------+-----+
| 32 | Piper | 5 |
| 779 | Georgia | 20 |
| 849 | Willow | 14 |
+------------+---------+-----+
Explanation: Student with id 217 havs empty value in the name column, so it will be removed.
Constraints
- The DataFrame will have at least 1 row
- The DataFrame will have at least 1 column
- Missing values are represented as NaN
Approach 1: Using dropna()
Intuition
Pandas provides the dropna() method which is specifically designed to remove missing values from a DataFrame. This is the most straightforward and idiomatic way to handle this problem.
Steps
- Call the
dropna()method on the DataFrame - By default,
dropna()removes rows containing any missing values - Return the resulting DataFrame
import pandas as pd
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
return students.dropna()Complexity
- Time: O(n × m) where n is the number of rows and m is the number of columns
- Space: O(n × m) for the resulting DataFrame
- Notes: This is the most efficient and readable approach for pandas DataFrames
Approach 2: Using Boolean Indexing
Intuition We can create a boolean mask that indicates which rows have no missing values, then use this mask to filter the DataFrame.
Steps
- Use
isna()to create a boolean DataFrame indicating missing values - Use
any()along axis 1 to find rows with at least one missing value - Negate the result to get rows with no missing values
- Use boolean indexing to filter the DataFrame
import pandas as pd
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
mask = ~students.isna().any(axis=1)
return students[mask]Complexity
- Time: O(n × m) where n is the number of rows and m is the number of columns
- Space: O(n × m) for the boolean mask and resulting DataFrame
- Notes: More verbose than dropna() but demonstrates understanding of boolean indexing
Approach 3: Using notnull()
Intuition
We can use the notnull() method to check for non-missing values and combine the results across all columns.
Steps
- Use
notnull()to create a boolean DataFrame indicating non-missing values - Use
all()along axis 1 to find rows where all values are non-missing - Use boolean indexing to filter the DataFrame
import pandas as pd
def dropMissingData(students: pd.DataFrame) -> pd.DataFrame:
mask = students.notnull().all(axis=1)
return students[mask]Complexity
- Time: O(n × m) where n is the number of rows and m is the number of columns
- Space: O(n × m) for the boolean mask and resulting DataFrame
- Notes: Similar to Approach 2 but uses the opposite logic (notnull vs isna)