My code is supposed to change the missing value to its valid immediate adjacent values. However, it does not seem to be running correctly and I'm not quite sure where the mistake lies.
So the missing value has to be corrected to the average of its adjacent values. However, if there are consecutive missing values, then they should be corrected with the valid adjacent value. Only basic python functions like for loops and if/else are to be used.
data = [1, -999, -999, 3, 4]
missing = -999
for i, num in enumerate(data):
if num == missing:
if [i-1] and [i+1] != missing:
data[i] = (data[i-1] + data[i+1]) / 2
elif [i-1] != missing and [i+1] == missing:
data[i] = data[i-1]
elif [i+1] != missing and [i-1] == missing:
data[i] = data[i+1]
else:
data[i] = data[i]
print(data)
For example:
data = [1, 2, -999, 4, 5], [1, -999, -999, 3, 4], [-999, -999, 3, 4, 5], [-999, -999, -999, -999, -999]
expected output = [1, 2, 3, 4, 5], [1, 1, 3, 3, 4], [3, 3, 3, 4, 5], [-999, -999, -999, -999, -999]
Aucun commentaire:
Enregistrer un commentaire