
Sponsored
Sponsored
This approach leverages a filtering method to iterate over each row and eliminate the rows with missing 'name' values. The method checks for nullity in the 'name' column and keeps only those rows where 'name' is not null.
Time Complexity: O(n), where n is the number of rows, as we potentially check each row.
Space Complexity: O(1), as we are modifying the DataFrame in place (though Pandas may create a copy depending on the operation).
1import pandas as pd
2
3def In Python, Pandas provides a convenient dropna function which is used to remove missing values. Here, we specify the 'name' column in the subset parameter to ensure only rows where the 'name' is missing are dropped.
This approach manually iterates over each row in the dataset, checking if the 'name' field is missing. Rows with missing 'name' values are filtered out, which can be useful in environments that lack high-level filtering functions.
Time Complexity: O(n), to iterate over each student.
Space Complexity: O(n), to store the valid students in the new array.
1#include <iostream>
2#include <vector>
3using namespace std;
4
5struct Student {
6 int student_id;
7 string name;
8 int age;
9};
10
11vector<Student> dropMissingData(const vector<Student>& students) {
12 vector<Student> validStudents;
13 for (const auto& student : students) {
14 if (!student.name.empty()) {
15 validStudents.push_back(student);
16 }
17 }
18 return validStudents;
19}
20In C++, we use a vector to store the students. We loop through each student and add those with non-empty names to a new vector called validStudents.