We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Drop Missing Data

Number: 3075

Difficulty: Easy

Paid? No

Companies: N/A


Problem Description

Given a table (or DataFrame) of students with columns student_id, name, and age, remove all rows where the value in the name column is missing.


Key Insights

  • The problem requires filtering out rows with missing values in the name column.
  • You can use built-in filtering methods available in most languages or libraries.
  • In Python (with pandas), the dropna method is a simple and efficient way to achieve this.
  • In other languages, iterating through the list of records and filtering manually is a common approach.

Space and Time Complexity

Time Complexity: O(n), where n is the number of rows in the DataFrame or list.
Space Complexity: O(n), in the worst case where a new list of filtered elements is created.


Solution

We iterate through the collection of student records and only keep those rows where the name is not missing. In data-centric languages like Python with pandas, built-in functions such as dropna can be used. In other languages, we can iterate through an array or vector of student objects and check if the name field is not null (or the equivalent representation of missing data). The main trick is to correctly identify the missing value (e.g., a null pointer or a 'None' value) and filter accordingly.


Code Solutions

# Importing pandas library
import pandas as pd

# Function to drop rows with missing 'name'
def drop_missing_data(students: pd.DataFrame) -> pd.DataFrame:
    # Use dropna to remove rows where 'name' is missing
    return students.dropna(subset=['name'])

# Example usage:
data = {
    "student_id": [32, 217, 779, 849],
    "name": ["Piper", None, "Georgia", "Willow"],
    "age": [5, 19, 20, 14]
}
students_df = pd.DataFrame(data)
result = drop_missing_data(students_df)
print(result)  # Expected to print the DataFrame without the row having None in 'name'
← Back to All Questions