python - Replace unwanted value with the preceding value in the list -


i have following signalid in dataframe(df). trying smooth signal while replacing unwanted signalid's

in following example, a) 6th id 03 should replaced 01 b) 12th , 13th (04 , 05 respectively) should replaced 02

01 01 01 01 01 03 01 01 02 02 02 04 05 02 02 02 

i can replace value if know exact location, how loop through this?

df.id.loc[6] = 01 

this code trick... assuming logic determine 3,4,5 invalid > 2.

you may need make adjustments logic find value out of place, rest of procedure below should work. find invalid value, set nan. ffill() @ end.

import pandas pd import numpy np data = pd.series([51,51,1,51,51,1,1,2,1,1,1,48,48,2,48,                   1,1,1,1,3,1,1,2,2,2,4,5,2,2,2])  # answer 2, 7, 13, 19, 25, 26  # size 1 gaps.  # next value,  data_pr = data.shift(-1) data_nx = data.shift(1)  # % exclude first 2 items filder.  data_nx[:1]= data[:1] data_pr[-1:] = data[-1:] #data_lag2[:2]= data[:2]  # % find idx data != data_lag1 && data == data_lag2 data[(data != data_pr) & (data != data_nx) & (data_pr == data_nx) ]=np.nan # % invalid values 2 in row...  data_pr2 = data.shift(-2)  data_nx2 = data.shift(2)    data[(data != data_pr) & (data != data_nx) & (data_pr2 == data_nx2) ]=np.nan # % assert (data[data.isnull()].index.values == np.array([2,7,13,19,25,26])).all() data.ffill(inplace=true) 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -