This is trivial in Excel, why is this so hard in Python?
The goal is to compute the state variable based on several conditions, including the previous value of the state variable as well. Value is the known integer, Min(3) and Max(3) are simply the minimum and maximum value of a 3 period rolling window shifted forward by one period. This is how far I got.
Index Value Max(3) Min(3)
0 10 nan nan
1 20 nan nan
2 15 nan nan
3 25 20 10
4 15 25 15
5 10 25 15
6 15 20 10
What is the best way to calculate the state variable based on the following conditions:
- a) If Value > Max(3) then 1
- b) If Value < Min(3) then 4
- c) If Value <= Max(3) & Value >= Min (3) & previous State = 1 or 2 then 2
- d) If Value <= Max(3) & Value >= Min (3) & previous State = 4 or 3 then 3
Should look like this in final DataFrame:
Index Value Max(3) Min(3) State
0 10 nan nan nan
1 20 nan nan nan
2 15 nan nan nan
3 25 20 10 1
4 15 25 15 2
5 10 25 15 4
6 15 20 10 3
I have mostly tried this using np.where() funtion but always run into problems once I approach c) and d) conditions.
