problemeasyalgorithmsleetcode-2883leetcode 2883leetcode2883

Drop Missing Data

EasyUpdated: Aug 2, 2025
Practice on:

Problem

DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | student_id | int | | name | object | | age | int | +-------------+--------+

There are some rows having missing values in the name column.

Write a solution to remove the rows with missing values.

The result format is in the following example.

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.

## Solution

### Method 1 – pandas dropna

#### Intuition

To remove rows with missing values in the `name` column, we can use the `dropna` method in pandas, specifying the `name` column.

#### Approach

1. Use `dropna` on the DataFrame, specifying `subset=['name']`.
2. Return the resulting DataFrame.

#### Code

{{< code_tabs >}}
##### Python
```python
import pandas as pd

def drop_missing_data(students: pd.DataFrame) -> pd.DataFrame:
    return students.dropna(subset=['name'])

Complexity

  • ⏰ Time complexity: O(n), where n is the number of rows in the DataFrame.
  • 🧺 Space complexity: O(n), for the output DataFrame.

Comments