I want to transfer SAS code to python, and cannot find a retain function in python.
The data is like :
type_id amount
1 100
1 200
1 400
2 0
1 200
1 300
2 0
1 150
What I want is when type_id = 2, the amount is equal to the negative value of the previous row. So the data will be like this after retain function:
type_id amount
1 100
1 200
1 400
2 -400
1 200
1 300
2 -300
1 150
The SAS code is :
data B;
set A;
retain tempvar 0;
if type_id = 2
then amount = tempvar;
else tempvar = -amount;
drop tempvar;
run;
Does anyone have any idea about how to do this in python? Thanks!