The problem consists in changing sequences of numbers between zeros, to the first value of the specific sequence.
Example:
| Input(df['p']) | Desired Output(df['Do']) |
|---|---|
| 0 | 0 |
| 0 | 0 |
| 0 | 0 |
| 115 | 115 |
| 090 | 115 |
| 0 | 0 |
| -80 | -80 |
| -90 | -80 |
| -70 | -80 |
| 0 | 0 |
I have tried using np.where, and logic to find the numbers that must be changed, though i'm not able to scale this solution. It only works for one row.
Example:
#check a change in value #eliminate the first change from zero #assigning the above p value
df['A'] =np.where(df['p']!=df['p'].shift(1),np.where(df['p'].shift(1)==0,df['p'],df['p'].shift(1)),df['p'])
| Input(df['p']) | Actual Output(df['A']) |
|---|---|
| 0 | 0 |
| 0 | 0 |
| 0 | 0 |
| 115 | 115 |
| 090 | 115 |
| 0 | 0 |
| -80 | -80 |
| -90 | -80 |
| -70 | -90 |
| 0 | 0 |
Something like that should work, but np.where doesn't support iterations between the same column.
#check a change in value #eliminate the first change from zero #assigning the above Do value
#\/ \/
df['Do'] = np.where(df['p']!=df['p'].shift(1),np.where(df['p'].shift(1)==0,df['p'],df['Do'].shift(1)),df['p'])
Tks!