I have several large data sets with sensor readings, where sometimes the line will be 0. Heuristic is quite simple: if the previous line and the next line were not 0, I assume that this is a sensor failure, and I replace this line with the middle of the two around him.
There are legitimate cases where the readings of the sensors can be 0, so just looking at 0s is not an option.
so far I have come up with the following method of cleaning it:
data["x+1"] = data["x"].shift(1)
data["x+2"] = data["x"].shift(2)
res = data[["x", "x+1", "x+2"]].apply(
lambda x : (x[0] + x[2])/2
if ((x[0] > 0) and (x[1] <= 0) and (x[2] > 0) )
else x[1], axis=1 )
data[x] = res.shift(-1)
This works in principle, and I prefer it to iterate over 3 shifted and shifted data frames as follows:
for row1, row2, row3 in zip( data.iterrows(), data.shift(1).iterrows(), data.shift(2).iterrows() ):
...
, . , apply ().
, :
data.loc[ data["x"] == 0 , "x" ] = np.NaN
data["x"].fillna( method="ffill", limit=1, inplace=True)
data["x"].fillna( 0 )
, , , ( NaN, , , NaN)
, , . awk , , python .
.