'Holding last value based on a condition in another columns
How can I hold the last value based on another column and repeat it until the condition is true I tried but it shows me an error
conditions = [
(df['position'] ==-1) & (df['signl']==0),
(df['position'] ==0) & (df['signl']==0),
(df['position'] ==1) & (df['signl']==1),
(df['position'] ==0) & (df['signl']==1)
]
choices = [df['open'],df['open'].shift(),df['open'],df['open'].shift()]
df['test'] = np.select(conditions,choices,default=None)
I want the output be like this :
open signl position test
42313.12 0 NaN None
42257 0 0 42313.12
42128.03 1 1 42128.03
42072.26 1 0 42128.03
42149 1 0 42128.03
42154.99 1 0 42128.03
42166.63 0 -1 42166.63
42143.01 0 0 42166.63
42192.13 0 0 42166.63
42195.23 0 0 42166.63
42144.94 0 0 42166.63
42156.52 1 1 42156.52
42169.98 1 0 42156.52
42088.72 1 0 42156.52
42097.33 1 0 42156.52
41995.75 1 0 42156.52
41954.59 0 -1 41954.59
41949.95 0 0 41954.59
42005.71 0 0 41954.59
41987.55 0 0 41954.59
41909.43 0 0 41954.59
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
