I have a Dataframe with the column "Trade" filled with some positive values; the rest are NaN values.
Trade
100
Nan
Nan
101
Nan
102
Nan
98
107
Nan
101
Nan
98
Nan
Nan
94
Among the non-Nan values, I need a vectorized solution to remove the values that fall within the 95%-105% value range of the last value that hasn't been removed. The final result should look like this:
Trade
100
Nan
Nan
Nan
Nan
Nan
Nan
Nan
107
Nan
101
Nan
Nan
Nan
Nan
94
As others have pointed out since there is a dependency between the algorithm and the resultant rows there won't be a pure "single-pass" vectorized solution. However, you can still take a "shrinking" window approach to solve this problem while trying to minimize the number of iterative steps required.
The reason I refer to this as a "shrinking" window approach is because each tick of our while-loop will work on a smaller chunk of the DataFrame, until we have consumed all of it. This allows us to leverage as many DataFrame/Series methods as possible so we're not doing any data processing at the Python level.