I've got a pandas dataframe. I want to 'lag' one of my column conditionally on an other columns. Typically below, i want to lag gdp based on y previous "real" value.
Typically first entry will be 0 because we are looking for y = 0.
Second entry will be 2 because we are looking for y = 1
Third entry will be zero because we are looking for y = 3
etc...
df =
y gdp cap
0 1 2 5
1 2 3 9
2 4 7 2
3 5 4 7
4 6 7 7
df_lag =
y gdp cap y_prev gdp_lag
0 1 2 5 0 0
1 2 3 9 1 2
2 4 7 2 3 0
3 5 4 7 4 7
4 6 7 7 5 4
Is there any simple way to do that?