I would like to write the following code in vector form since the current code is rather slow (and would like to learn Python best practices). Basically, the code says that if today the value is within 10% of yesterday's value, then today the value (in the new column) coincides with the value yesterday. Otherwise, today the value does not change:
def test(df): df['OldCol']=(100,115,101,100,99,70,72,75,78,80,110) df['NewCol']=df['OldCol'] for i in range(1,len(df)-1): if df['OldCol'][i]/df['OldCol'][i-1]>0.9 and df['OldCol'][i]/df['OldCol'][i-1]<1.1: df['NewCol'][i]=df['NewCol'][i-1] else: df['NewCol'][i]=df['OldCol'][i] return df['NewCol']
The output should be as follows:
OldCol NewCol 0 100 100 1 115 115 2 101 101 3 100 101 4 99 101 5 70 70 6 72 70 7 75 70 8 78 70 9 80 70 10 110 110
Can you help?
I would like to use something like this, but I was not able to solve my problem:
def test(df): df['NewCol']=df['OldCol'] cond=np.where((df['OldCol'].shift(1)/df['OldCol']>0.9) & (df['OldCol'].shift(1)/df['OldCol']<1.1)) df['NewCol'][cond[0]]=df['NewCol'][cond[0]-1] return df
source share