python - Pandas self-conditional column -
i trying create column in pandas conditional it's previous value along other columns.
import pandas pd import numpy np = np.random.standard_normal(100) = pd.dataframe(a) a['out'] = 0 a['out2'] = 0 t in range(1,a.shape[0]): if (a[0][t] > 1) & (a['out'][t-1]==0): a['out'][t] = 1 elif (a[0][t] < -1) & (a['out'][t-1]==0): a['out'][t] = -1 elif ((a[0][t] > 0) & (a['out'][t-1]==-1)) | ((a[0][t] < 0) & (a['out'][t-1]==1)): a['out'][t] = 0 else: a['out'][t] = a['out'][t-1] a['out2'] = np.where((a.index== 0),0 ,np.where((a[0] > 1) & (a['out2'].shift()==0), 1 ,np.where((a[0] < -1) & (a['out2'].shift()==0), -1 ,np.where(((a[0] > 0) & (a['out2'].shift()==-1)) | ((a[0] < 0) & (a['out2'].shift()==1)), 0 ,a['out2'].shift()))))
the column a['out2'] tries copy a['out'] in vectorized form not read previous values. column a['out'] takes long compile through loop. can me faster, vectorized approach creating column?
you can create function , use apply. access previous data can use variable store value. hope following code helps.
import pandas pd import numpy np = np.random.standard_normal(100) = pd.dataframe(a) state = 0 def get_val(a,prev_state): global state if (a > 1) & (prev_state==0): state = 1 elif (a < -1) & (prev_state==0): state = -1 elif ((a > 0) & (prev_state==-1)) | ((a < 0) & (prev_state==1)): state = 0 return state a['out'] = a[0].apply(lambda x: get_val(x,state))
output :
0 out 0 1.366864 1 1 0.887763 1 2 -0.663636 0 3 -1.824950 -1 4 0.459663 0 5 -1.325129 -1 6 1.587188 0 7 -0.148159 0 8 0.578862 0 9 0.758460 0
if use %%timeit
100 loops, best of 3: 2.16 ms per loop
Comments
Post a Comment