Here below a dataframe.
df_strat = pd.DataFrame({'in_pos': [0, 0, 0, 1, 0, 0, -1, 0, 0, 0],
'in': [0, 0, 0, 1, 0, 0, -1, 0, 0, 0]})
I need to update the 'in' column value, either when there is a '1' in the column 'in_pos' or when there is a '1' in the previous row of the 'in' column. Same when values are -1 and -1. And I would extend to any set of value.
So I used the following code only to test '1' values:
df_strat.loc[(df_strat['in_pos'].shift(1) == 1) | (df_strat['in'].shift(1) == 1), 'in'] = 1
I expect a recursive update using the loc method.
But instead, I get only a single row update as below.
I tried the following too :
conditions = [
(df_strat['in_pos'] == 1) | (df_strat['in'].shift(1).fillna(0) == 1),
(df_strat['in_pos'] == -1) | (df_strat['in'].shift(1).fillna(0) == -1),
(df_strat['in_pos'] == 0) | (df_strat['in'].shift(1).fillna(0) == 0)]
df_strat['in'] = np.select(conditions, [1, -1, 0], default=df_strat['in'])
but result is a below
Expected result is
df_expected = pd.DataFrame({'in_pos': [0, 0, 0, 1, 0, 0, -1,0,0,0],
'in': [0, 0, 0, 1, 1, 1, -1, -1, -1,-1]})
Can anyone help ? thks.

