
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 drop_missing_dataIn 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 <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5typedef struct {
6 int student_id;
7 char *name;
8 int age;
9} Student;
10
11Student* dropMissingData(Student* students, int size, int* newSize) {
12 Student* validStudents = malloc(size * sizeof(Student));
13 int index = 0;
14 for (int i = 0; i < size; i++) {
15 if (students[i].name != NULL) {
16 validStudents[index++] = students[i];
17 }
18 }
19 *newSize = index;
20 return validStudents;
21}
22This C program defines a structure Student and iterates over each student, checking if the name is NULL. Valid entries are copied to a new array, which is returned after the loop.