Let's say I've the below data frame:
df = pd.DataFrame(
np.random.randn(10, 3),
index=pd.date_range("1/1/2022", freq="D", periods=10),
columns=["A", "B", "C"],
)
df
I want to add column D
, so that we check the value of B
at each date, if it is positive then D
equals B
of the previous day, if it is negative, then it is max of A
and C
of the previous day.
For example:
- At 2022-01-05, B is positive, then D should be equal to the value of B at day 2022-01-04 that is
-1.329675
- At 2022-01-03, B is negative, then D should be equal to the max of A and C at day 2022-01-02 that is
0.562377